Skip to content

Commit

Permalink
Export the Path variable rather than appending to it
Browse files Browse the repository at this point in the history
The `addPath` will invert the path ordering as each entry is appended to the
environment file, which is then reversed and prepended to the environment at the
end of the action.  This results in the additions being in reverse order.
However, since we know that `vsdevcmd` will adjust the path on both ends, simply
prefer to use the `exportVariable` rather than try to be clever to adjust the
path via `addPath`.

Fixes: seanmiddleditch#10.
  • Loading branch information
compnerd authored Oct 23, 2021
1 parent 6922ba6 commit c674eb5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
steps:
- uses: actions/checkout@master
- uses: actions/setup-node@v1
- uses: ./
- uses: compnerd/gha-setup-vsdevenv@path-management
- run: 'cl /EHsc /Fe:test.exe tests\\test.cpp && .\\test.exe'
if: runner.os == 'Windows'

31 changes: 14 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ const process = require('process')
const path = require('path')
const spawn = require('child_process').spawnSync

core.setFailed("fatal error")
return

try {
// this job has nothing to do on non-Windows platforms
if (process.platform != 'win32') {
Expand Down Expand Up @@ -55,12 +58,19 @@ try {
if (winsdk != '')
vsDevCmdArgs.push(`-winsdk=${winsdk}`)

const cmdArgs = [ '/q', '/k'].concat(vsDevCmdArgs, ['&&', 'set'])

const cmdArgs = [ '/q', '/k' ].concat(vsDevCmdArgs, ['&&', 'set'])
console.log(`$ cmd ${cmdArgs.join(' ')}`)

console.error("FAILURE")
core.setFailed("fatal error")
return


console.log("running ...")
const cmdResult = spawn('cmd', cmdArgs, {encoding: 'utf8'})
console.log(`cmdResult: ${cmdResult}`)
if (cmdResult.error) throw cmdResult.error

const cmdOutput = cmdResult.output
.filter(s => !!s)
.map(s => s.split('\n'))
Expand All @@ -71,25 +81,12 @@ try {
const completeEnv = cmdOutput
.filter(s => s.indexOf('=') != -1)
.map(s => s.split('=', 2))
const filteredEnv = completeEnv
.filter(([key, _]) => key != 'Path' && !process.env[key])

const filteredEnv = completeEnv.filter(!process.env[key])
for (const [key, value] of filteredEnv) {
console.log(`Setting ${key} => ${value}`)
core.exportVariable(key, value)
}

const pathEntries = process.env['Path'].split(';')
const newPathEntries = completeEnv
.filter(([key, _]) => key == 'Path')
.map(([_, value]) => value)
.join(';')
.split(';')
.filter(path => pathEntries.indexOf(path) == -1)

for (const path of newPathEntries) {
core.addPath(path)
}

console.log('environment updated')
} catch (error) {
core.setFailed(error.message);
Expand Down

0 comments on commit c674eb5

Please sign in to comment.