From 54220dd92c06e7da112b139f95d8beb933e4cdde Mon Sep 17 00:00:00 2001 From: Ryan Bas <18267769+ryanbas21@users.noreply.github.com> Date: Tue, 27 Jan 2026 15:14:11 -0700 Subject: [PATCH] Conditionally append `NPM_TOKEN` to `.npmrc` (#545) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: conditionally append NPM_TOKEN to .npmrc for trusted publishing support The .npmrc generation now intelligently handles both traditional NPM token authentication and trusted publishing scenarios: - Only appends auth token to .npmrc when NPM_TOKEN is defined (not undefined) - Uses strict comparison (!== undefined) instead of truthy checks - Provides informative logging when no NPM_TOKEN is present - Maintains full backward compatibility with existing workflows This fixes issues with trusted publishing where OIDC tokens from GitHub Actions are used instead of NPM_TOKEN, preventing 'undefined' from being written to the registry auth configuration. đŸ€– Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude * Update src/index.ts Co-authored-by: Mateusz BurzyƄski * simplify * tweak changeset --------- Co-authored-by: Claude Co-authored-by: Mateusz BurzyƄski --- .changeset/npmrc-trusted-publishing.md | 5 +++ src/index.ts | 47 +++++++++++++++----------- 2 files changed, 33 insertions(+), 19 deletions(-) create mode 100644 .changeset/npmrc-trusted-publishing.md diff --git a/.changeset/npmrc-trusted-publishing.md b/.changeset/npmrc-trusted-publishing.md new file mode 100644 index 0000000..f9987fe --- /dev/null +++ b/.changeset/npmrc-trusted-publishing.md @@ -0,0 +1,5 @@ +--- +"@changesets/action": patch +--- + +The `.npmrc` generation now intelligently handles both traditional NPM token authentication and trusted publishing scenarios by only appending the auth token when `NPM_TOKEN` is defined. This prevents 'undefined' from being written to the registry configuration when using OIDC tokens from GitHub Actions trusted publishing. diff --git a/src/index.ts b/src/index.ts index 1b0f63a..2f05db4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -67,32 +67,41 @@ const getOptionalInput = (name: string) => core.getInput(name) || undefined; "No changesets found. Attempting to publish any unpublished packages to npm" ); - let userNpmrcPath = `${process.env.HOME}/.npmrc`; - if (await fileExists(userNpmrcPath)) { - core.info("Found existing user .npmrc file"); - const userNpmrcContent = await fs.readFile(userNpmrcPath, "utf8"); - const authLine = userNpmrcContent.split("\n").find((line) => { - // check based on https://github.com/npm/cli/blob/8f8f71e4dd5ee66b3b17888faad5a7bf6c657eed/test/lib/adduser.js#L103-L105 - return /^\s*\/\/registry\.npmjs\.org\/:[_-]authToken=/i.test(line); - }); - if (authLine) { - core.info( - "Found existing auth token for the npm registry in the user .npmrc file" - ); + if (process.env.NPM_TOKEN) { + const userNpmrcPath = `${process.env.HOME}/.npmrc`; + + if (await fileExists(userNpmrcPath)) { + core.info("Found existing user .npmrc file"); + const userNpmrcContent = await fs.readFile(userNpmrcPath, "utf8"); + const authLine = userNpmrcContent.split("\n").find((line) => { + // check based on https://github.com/npm/cli/blob/8f8f71e4dd5ee66b3b17888faad5a7bf6c657eed/test/lib/adduser.js#L103-L105 + return /^\s*\/\/registry\.npmjs\.org\/:[_-]authToken=/i.test(line); + }); + if (authLine) { + core.info( + "Found existing auth token for the npm registry in the user .npmrc file" + ); + } else { + core.info( + "Didn't find existing auth token for the npm registry in the user .npmrc file, creating one" + ); + await fs.appendFile( + userNpmrcPath, + `\n//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n` + ); + } } else { core.info( - "Didn't find existing auth token for the npm registry in the user .npmrc file, creating one" + "No user .npmrc file found, creating one with NPM_TOKEN used as auth token" ); - await fs.appendFile( + await fs.writeFile( userNpmrcPath, - `\n//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n` + `//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n` ); } } else { - core.info("No user .npmrc file found, creating one"); - await fs.writeFile( - userNpmrcPath, - `//registry.npmjs.org/:_authToken=${process.env.NPM_TOKEN}\n` + core.info( + "No NPM_TOKEN found - assuming trusted publishing or npm is already authenticated" ); }