claranet / terraform-aws-lambda

Terraform module for AWS Lambda functions
MIT License
157 stars 126 forks source link

Leading quote in `source_path` causing failures on Windows #43

Closed lorengordon closed 5 years ago

lorengordon commented 5 years ago

Discovered an issue in the new custom build script implementation that causes failures on Windows... I believe there are some differences in how shells or sys.argv are interpreting arguments with quotes around them. Here is the error:

null_resource.archive (local-exec): Traceback (most recent call last):
null_resource.archive (local-exec):   File "build.py", line 129, in <module>
null_resource.archive (local-exec):     with cd(source_dir):
null_resource.archive (local-exec):   File "C:\Python36\lib\contextlib.py", line 81, in __enter__
null_resource.archive (local-exec):     return next(self.gen)
null_resource.archive (local-exec):   File "build.py", line 23, in cd
null_resource.archive (local-exec):     os.chdir(path)
null_resource.archive (local-exec): OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: ''C:\\...

You can see the leading ' in the directory name, there at the end. Note that there is no closing quote. I'm obviously removing the full path, but there is indeed no closing quote by the time the value gets to os.chdir(path).

I threw in some debug statements to see where there are differences between linux and windows, and I can see that hash.py is outputting the json the same way on both (thankfully). The difference appears to be where build.py is reading the arguments from sys.argv.

diff --git a/build.py b/build.py
index c32737e..38de3ad 100644
--- a/build.py
+++ b/build.py
@@ -108,6 +108,7 @@ filename = sys.argv[1]
 runtime = sys.argv[2]
 source_path = sys.argv[3]

+print('source_path = {0}'.format(source_path))
 absolute_filename = os.path.abspath(filename)

 # Create a temporary directory for building the archive,

On linux, that will output the following, with no quotes at all:

null_resource.archive (local-exec): source_path = /home/...

On windows, it outputs the following, complete with opening and closing quotes:

null_resource.archive (local-exec): source_path = 'C:\...'

Some further debugging reveals that the closing quote is stripped by source_dir = os.path.dirname(source_path).

The behavior of os.path.dirname is the same on windows and linux, so the failure is simply due to how the value of sys.argv[3] is not wrapped in quotes on linux but is wrapped in quotes on windows. The quotes come from the default value in variables.tf.

Still noodling possible fixes...