Skip to content

Commit

Permalink
fix(cli): patch missing SYSTEMROOT env var on windows (#4221)
Browse files Browse the repository at this point in the history
  • Loading branch information
rexxars committed Mar 1, 2023
1 parent 2123f93 commit 2b9beaf
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/@sanity/cli/src/cli.ts
@@ -1,4 +1,5 @@
/* eslint-disable no-console, no-process-exit, no-sync */
import os from 'os'
import path from 'path'
import {existsSync} from 'fs'
import chalk from 'chalk'
Expand Down Expand Up @@ -36,6 +37,7 @@ export async function runCli(cliRoot: string, {cliVersion}: {cliVersion: string}
}

loadAndSetEnvFromDotEnvFiles({workDir, cmd: args.groupOrCommand})
maybeFixMissingWindowsEnvVar()

// Check if there are updates available for the CLI, and notify if there is
await runUpdateCheck({pkg, cwd, workDir}).notify()
Expand Down Expand Up @@ -240,3 +242,21 @@ function loadAndSetEnvFromDotEnvFiles({workDir, cmd}: {workDir: string; cmd: str
process.env = {...process.env, ...studioEnv}
/* eslint-disable no-process-env */
}

/**
* Apparently, Windows environment variables are supposed to be case-insensitive,
* (https://nodejs.org/api/process.html#processenv). However, it seems they are not?
* `process.env.SYSTEMROOT` is sometimes `undefined`, whereas `process.env.SystemRoot` is _NOT_.
*
* The `open` npm module uses the former to open a browser on Powershell, and Sindre seems
* unwilling to fix it (https://github.com/sindresorhus/open/pull/299#issuecomment-1447587598),
* so this is a (temporary?) workaround in order to make opening browsers on windows work,
* which several commands does (`sanity login`, `sanity docs` etc)
*/
function maybeFixMissingWindowsEnvVar() {
/* eslint-disable no-process-env */
if (os.platform() === 'win32' && !('SYSTEMROOT' in process.env) && 'SystemRoot' in process.env) {
process.env.SYSTEMROOT = process.env.SystemRoot
}
/* eslint-enable no-process-env */
}

1 comment on commit 2b9beaf

@vercel
Copy link

@vercel vercel bot commented on 2b9beaf Mar 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

test-studio – ./

test-studio-git-next.sanity.build
test-studio.sanity.build

Please sign in to comment.