fix: pnpm setup breaks PATH with non-ascii characters (#4699)

Previous `pnpm setup` would break %PATH% containing non-ascii characters
Now it works fine

close #4698
This commit is contained in:
liuxingbaoyu
2022-05-09 00:29:38 +08:00
committed by GitHub
parent 32591b2c11
commit 7c9362d3dc
9 changed files with 15 additions and 8 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/plugin-commands-setup": patch
---
fix `pnpm setup` breaks %PATH% with non-ascii characters [#4698](https://github.com/pnpm/pnpm/issues/4698)

View File

@@ -8,7 +8,9 @@ export async function setupWindowsEnvironmentPath (pnpmHomeDir: string): Promise
const pnpmHomeRegex = /^ {4}(?<name>PNPM_HOME) {4}(?<type>\w+) {4}(?<data>.*)$/gim
const regKey = 'HKEY_CURRENT_USER\\Environment'
const queryResult = await execa('reg', ['query', regKey])
// Use `chcp` to make `reg` use utf8 encoding for output.
// Otherwise, the non-ascii characters in the environment variables will become garbled characters.
const queryResult = await execa(`chcp 65001>nul && reg query ${regKey}`, undefined, { shell: true })
if (queryResult.failed) {
return 'Win32 registry environment values could not be retrieved'

View File

@@ -34,6 +34,6 @@ test('Win32 registry environment values could not be retrieved', async () => {
pnpmHomeDir: __dirname,
})
expect(execa).toHaveBeenNthCalledWith(1, 'reg', ['query', regKey])
expect(execa).toHaveBeenNthCalledWith(1, `chcp 65001>nul && reg query ${regKey}`, undefined, { shell: true })
expect(output).toContain('Win32 registry environment values could not be retrieved')
})

View File

@@ -37,6 +37,6 @@ test('Environment PATH is not configured correctly', async () => {
pnpmHomeDir: __dirname,
})
expect(execa).toHaveBeenNthCalledWith(1, 'reg', ['query', regKey])
expect(execa).toHaveBeenNthCalledWith(1, `chcp 65001>nul && reg query ${regKey}`, undefined, { shell: true })
expect(output).toContain('Current PATH is not set. No changes to this environment variable are applied')
})

View File

@@ -40,6 +40,6 @@ HKEY_CURRENT_USER\\Environment
pnpmHomeDir: __dirname,
})
expect(execa).toHaveBeenNthCalledWith(1, 'reg', ['query', regKey])
expect(execa).toHaveBeenNthCalledWith(1, `chcp 65001>nul && reg query ${regKey}`, undefined, { shell: true })
expect(output).toContain('Current PATH is empty. No changes to this environment variable are applied')
})

View File

@@ -49,7 +49,7 @@ HKEY_CURRENT_USER\\Environment
pnpmHomeDir: __dirname,
})
expect(execa).toHaveBeenNthCalledWith(1, 'reg', ['query', regKey])
expect(execa).toHaveBeenNthCalledWith(1, `chcp 65001>nul && reg query ${regKey}`, undefined, { shell: true })
expect(execa).toHaveBeenNthCalledWith(2, 'reg', ['add', regKey, '/v', 'PNPM_HOME', '/t', 'REG_EXPAND_SZ', '/d', __dirname, '/f'])
expect(execa).toHaveBeenNthCalledWith(3, 'reg', ['add', regKey, '/v', 'Path', '/t', 'REG_EXPAND_SZ', '/d', `${__dirname};${currentPathInRegistry}`, '/f'])
expect(execa).toHaveBeenNthCalledWith(4, 'setx', ['PNPM_HOME', __dirname])

View File

@@ -46,7 +46,7 @@ HKEY_CURRENT_USER\\Environment
pnpmHomeDir: __dirname,
})
expect(execa).toHaveBeenNthCalledWith(1, 'reg', ['query', regKey])
expect(execa).toHaveBeenNthCalledWith(1, `chcp 65001>nul && reg query ${regKey}`, undefined, { shell: true })
expect(execa).toHaveBeenNthCalledWith(2, 'reg', ['add', regKey, '/v', 'Path', '/t', 'REG_EXPAND_SZ', '/d', `${'.pnpm\\home'};${currentPathInRegistry}`, '/f'])
expect(execa).toHaveBeenNthCalledWith(3, 'setx', ['PNPM_HOME', '.pnpm\\home'])
expect(output).toContain(`Currently 'PNPM_HOME' is set to '${'.pnpm\\home'}'`)

View File

@@ -42,7 +42,7 @@ HKEY_CURRENT_USER\\Environment
pnpmHomeDir: __dirname,
})
expect(execa).toHaveBeenNthCalledWith(1, 'reg', ['query', regKey])
expect(execa).toHaveBeenNthCalledWith(1, `chcp 65001>nul && reg query ${regKey}`, undefined, { shell: true })
expect(output).toContain(`Currently 'PNPM_HOME' is set to '${'.pnpm\\home'}'`)
expect(output).toContain('PATH already contains PNPM_HOME')
})

View File

@@ -49,7 +49,7 @@ HKEY_CURRENT_USER\\Environment
pnpmHomeDir: __dirname,
})
expect(execa).toHaveBeenNthCalledWith(1, 'reg', ['query', regKey])
expect(execa).toHaveBeenNthCalledWith(1, `chcp 65001>nul && reg query ${regKey}`, undefined, { shell: true })
expect(execa).toHaveBeenNthCalledWith(2, 'reg', ['add', regKey, '/v', 'PNPM_HOME', '/t', 'REG_EXPAND_SZ', '/d', __dirname, '/f'])
expect(execa).toHaveBeenNthCalledWith(3, 'reg', ['add', regKey, '/v', 'Path', '/t', 'REG_EXPAND_SZ', '/d', `${__dirname};${currentPathInRegistry}`, '/f'])
expect(output).toContain(`Setting 'PNPM_HOME' to value '${__dirname}`)