Skip to content

Commit a1d43cb

Browse files
committedSep 14, 2022
Convert absolute paths used in tasks to Windows format
The Task task runner tool uses an integrated Bash shell interpreter to allow the use of standard POSIX/Bash syntax and built-in utilities while providing cross-platform support for users who have made the poor choice of the non-standard Windows cmd or PowerShell shells. While this is a nice feature of Task, distinguishing it from other alternatives, it is still often necessary to use non-built-in utilities. So use of a Bash shell is a requirement to run our tasks even the developers using Windows. Although high quality Bash shells for Windows such as the Git Bash included as part of the Git for Windows installation generally provide a seamless experience, there is the occasional "gotcha". Relative paths work the same for POSIX and Windows, but absolute POSIX format paths may not. These paths are handled as expected by Task's integrated shell interpreter and by Bash, but when these paths are used outside that context they may no longer be correct. For example: ``` foo: cmds: - mkdir "/tmp/posix-path-test-dir" - touch "/tmp/posix-path-test-dir/posix-path-test-file" - ls "/tmp/posix-path-test-dir" - cd "/tmp/posix-path-test-dir" ``` The first three commands work as expected, but the `cd` command fails: ``` $ task foo task: [foo] mkdir "/tmp/posix-path-test-dir" task: [foo] touch "/tmp/posix-path-test-dir/posix-path-test-file" task: [foo] ls "/tmp/posix-path-test-dir" posix-path-test-file task: [foo] cd "/tmp/posix-path-test-dir" task: Failed to run task "foo": exit status 1 ``` The POSIX format `/tmp` is actually set to the system temporary directory: ``` $ cygpath -w "/tmp/posix-path-test-dir" C:\Users\per\AppData\Local\Temp\posix-path-test-dir ``` But if treated as a Windows format path, this would be the `tmp` subfolder of the root of the current drive (e.g., `C:\tmp`). Even though the command was run from Git Bash, which provides a `cd` that handles this absolute path perfectly, when run from Task the Windows native `cd` is used instead. This resulted in several of the data file validation tasks, which download the JSON schema to the temporary folder, to fail when ran on Windows. The solution is to convert these absolute paths, the occurrence of which are relatively rare in our tasks, to Windows format (which is handled fine by both the integrated, Bash, and external applications) when the task is ran on a Windows machine. The cygpath utility provides this capability: ``` $ task foo task: [foo] mkdir "C:\Users\per\AppData\Local\Temp/posix-path-test-dir" task: [foo] touch "C:\Users\per\AppData\Local\Temp/posix-path-test-dir/posix-path-test-file" task: [foo] ls "C:\Users\per\AppData\Local\Temp/posix-path-test-dir" posix-path-test-file task: [foo] cd "C:\Users\per\AppData\Local\Temp/posix-path-test-dir" ```
1 parent 70eaf8e commit a1d43cb

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed
 

‎Taskfile.yml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ tasks:
99
# Source: https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/github-workflow.json
1010
WORKFLOW_SCHEMA_URL: https://json.schemastore.org/github-workflow
1111
WORKFLOW_SCHEMA_PATH:
12-
sh: mktemp -t workflow-schema-XXXXXXXXXX.json
12+
sh: task utility:mktemp-file TEMPLATE="workflow-schema-XXXXXXXXXX.json"
1313
WORKFLOWS_DATA_PATH: "./.github/workflows/*.{yml,yaml}"
1414
cmds:
1515
- |
@@ -216,6 +216,30 @@ tasks:
216216
fi
217217
- shfmt -w .
218218

219+
# Make a temporary file named according to the passed TEMPLATE variable and print the path passed to stdout
220+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/windows-task/Taskfile.yml
221+
utility:mktemp-file:
222+
vars:
223+
RAW_PATH:
224+
sh: mktemp --tmpdir "{{.TEMPLATE}}"
225+
cmds:
226+
- task: utility:normalize-path
227+
vars:
228+
RAW_PATH: "{{.RAW_PATH}}"
229+
230+
# Print a normalized version of the path passed via the RAW_PATH variable to stdout
231+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/windows-task/Taskfile.yml
232+
utility:normalize-path:
233+
cmds:
234+
- |
235+
if [[ "{{.OS}}" == "Windows_NT" ]] && which cygpath &>/dev/null; then
236+
# Even though the shell handles POSIX format absolute paths as expected, external applications do not.
237+
# So paths passed to such applications must first be converted to Windows format.
238+
cygpath -w "{{.RAW_PATH}}"
239+
else
240+
echo "{{.RAW_PATH}}"
241+
fi
242+
219243
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-yaml-task/Taskfile.yml
220244
yaml:lint:
221245
desc: Check for problems with YAML files

0 commit comments

Comments
 (0)
Please sign in to comment.