Loading...
Loading...
### Terraform Version ``` Terraform v1.0.8 on linux_amd64 + provider registry.terraform.io/hashicorp/aws v3.59.0 + provider registry.terraform.io/hashicorp/external v2.1.0 + provider registry.terraform.io/hashicorp/null v3.1.0 + provider registry.terraform.io/hashicorp/template v2.2.0 + provider registry.terraform.io/hashicorp/tls v3.1.0 ``` Verified on Ubuntu 20.04 and Mac OS ### Steps to Reproduce See this example (where `~/bar` contains 1.txt, 2.txt, and `~/baz` contains 3.txt, 4.txt) ... Note that the source for `bar` has no trailing slash but the source for `baz` does ``` provisioner "remote-exec" { inline = ["mkdir -p /tmp/foo"] } provisioner "file" { source = "~/bar" destination = "/tmp/foo" } provisioner "file" { source = "~/baz/" destination = "/tmp/foo" } provisioner "remote-exec" { inline = [ "ls -lR /tmp/foo", "rm -r /tmp/foo", ] } ``` ... we get... ``` /tmp/foo: total 8 drwxr-xr-x 2 ubuntu ubuntu 4096 Sep 30 21:27 bar drwxr-xr-x 2 ubuntu ubuntu 4096 Sep 30 21:27 baz /tmp/foo/bar: total 8 -rw-r--r-- 1 ubuntu ubuntu 4 Sep 30 21:27 1.txt -rw-r--r-- 1 ubuntu ubuntu 4 Sep 30 21:27 2.txt /tmp/foo/baz: total 8 -rw-r--r-- 1 ubuntu ubuntu 6 Sep 30 21:27 3.txt -rw-r--r-- 1 ubuntu ubuntu 5 Sep 30 21:27 4.txt ``` ### Expected Behavior The contents of `baz` should be copied directly to `/tmp` ### Actual Behavior `baz` and it's contents are copied to `/tmp/baz` When however we do this... ``` provisioner "remote-exec" { inline = ["mkdir -p /tmp/foo"] } provisioner "file" { source = "/home/jimmy/bar" destination = "/tmp/foo" } provisioner "file" { source = "/home/jimmy/baz/" destination = "/tmp/foo" } provisioner "remote-exec" { inline = [ "ls -lR /tmp/foo", "rm -r /tmp/foo", ] } ``` The results are correct i.e. ... ``` /tmp/foo: total 12 -rw-r--r-- 1 ubuntu ubuntu 6 Sep 30 14:34 3.txt -rw-r--r-- 1 ubuntu ubuntu 5 Sep 30 14:34 4.txt drwxr-xr-x 2 ubuntu ubuntu 4096 Sep 30 14:34 bar /tmp/foo/bar: total 8 -rw-r--r-- 1 ubuntu ubuntu 4 Sep 30 14:34 1.txt -rw-r--r-- 1 ubuntu ubuntu 4 Sep 30 14:34 2.txt ``` ### Analysis It looks like [getSrc()](https://github.com/hashicorp/terraform/blob/0062e7112aac83304ea906f08c5aeb89b4ebfefa/internal/builtin/provisioners/file/resource_provisioner.go#L126) calls [homedir.Expand](https://github.com/mitchellh/go-homedir/blob/af06845cf3004701891bf4fdb884bfe4920b3727/homedir.go#L58), which calls, [filepath.Join()](https://pkg.go.dev/path/filepath#Join) which cleans up the path by removing any empty segments (and trailing slashes) Using... ``` provisioner "file" { source = pathexpand("~/bar/") destination = "/tmp/foo" } ``` ... fails in the same way because `pathexpand` also [calls](https://github.com/hashicorp/terraform/blob/d35bc0531255b496beb5d932f185cbcdb2d61a99/internal/lang/funcs/filesystem.go#L350) homedir.Expand ### Debug Output ``` null_resource.bastion_provisioning: Provisioning with 'file'... 2021-09-30T21:27:23.027Z [INFO] using private key for authentication 2021-09-30T21:27:23.037Z [DEBUG] Connecting to 52.209.199.110:22 for SSH 2021-09-30T21:27:23.073Z [DEBUG] Connection established. Handshaking for user ubuntu 2021-09-30T21:27:23.325Z [DEBUG] starting ssh KeepAlives 2021-09-30T21:27:23.325Z [DEBUG] Uploading dir '/home/adajoh01/bar' to '/tmp/foo' 2021-09-30T21:27:23.326Z [DEBUG] opening new ssh session 2021-09-30T21:27:23.824Z [DEBUG] Starting remote scp process: scp -rvt /tmp/foo 2021-09-30T21:27:23.852Z [DEBUG] Started SCP session, beginning transfers... 2021-09-30T21:27:23.853Z [DEBUG] No trailing slash, creating the source directory name 2021-09-30T21:27:23.853Z [DEBUG] SCP: starting directory upload: bar 2021-09-30T21:27:23.860Z [DEBUG] Beginning file upload... 2021-09-30T21:27:23.886Z [DEBUG] Beginning file upload... 2021-09-30T21:27:23.916Z [DEBUG] SCP session complete, closing stdin pipe. 2021-09-30T21:27:23.918Z [DEBUG] Waiting for SSH session to complete. 2021-09-30T21:27:23.989Z [ERROR] scp stderr: "Sink: D0755 0 bar\nSink: C0644 4 2.txt\nSink: C0644 4 1.txt\nSink: E\n" null_resource.bastion_provisioning: Provisioning with 'file'... 2021-09-30T21:27:23.994Z [INFO] using private key for authentication 2021-09-30T21:27:23.998Z [DEBUG] Connecting to 52.209.199.110:22 for SSH 2021-09-30T21:27:24.024Z [DEBUG] Connection established. Handshaking for user ubuntu 2021-09-30T21:27:24.259Z [DEBUG] starting ssh KeepAlives 2021-09-30T21:27:24.260Z [DEBUG] Uploading dir '/home/adajoh01/baz' to '/tmp/foo' 2021-09-30T21:27:24.260Z [DEBUG] opening new ssh session 2021-09-30T21:27:24.847Z [DEBUG] Starting remote scp process: scp -rvt /tmp/foo 2021-09-30T21:27:24.881Z [DEBUG] Started SCP session, beginning transfers... 2021-09-30T21:27:24.881Z [DEBUG] No trailing slash, creating the source directory name 2021-09-30T21:27:24.881Z [DEBUG] SCP: starting directory upload: baz 2021-09-30T21:27:24.881Z [DEBUG] Beginning file upload... 2021-09-30T21:27:24.906Z [DEBUG] Beginning file upload... 2021-09-30T21:27:24.941Z [DEBUG] SCP session complete, closing stdin pipe. 2021-09-30T21:27:24.942Z [DEBUG] Waiting for SSH session to complete. 2021-09-30T21:27:25.004Z [ERROR] scp stderr: "Sink: D0755 0 baz\nSink: C0644 5 4.txt\nSink: C0644 6 3.txt\nSink: E\n" ```
Click on a version to see all relevant bugs
Terraform Integration
Learn more about where this data comes from
Bug Scrub Advisor
Streamline upgrades with automated vendor bug scrubs
BugZero Enterprise
Wish you caught this bug sooner? Get proactive today.