Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .chronus/changes/forward-typespec-npm-registry-2026-08-15.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/compiler"
---

Forward `TYPESPEC_NPM_REGISTRY` to the npm process launched by `tsp install`.
8 changes: 6 additions & 2 deletions packages/compiler/src/install/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { createDiagnosticCollector } from "../core/diagnostics.js";
import { getDirectoryPath, joinPaths } from "../core/path-utils.js";
import { NoTarget, type Diagnostic, type Tracer } from "../core/types.js";
import { downloadAndExtractPackage } from "../package-manger/npm-package-download.js";
import { fetchPackageManifest, type NpmManifest } from "../package-manger/npm-registry.js";
import {
fetchPackageManifest,
getNpmRegistryEnvironment,
type NpmManifest,
} from "../package-manger/npm-registry.js";
import { mkTempDir } from "../utils/fs-utils.js";
import type { SupportedPackageManager } from "./config.js";
import { getPackageManagerConfig, type PackageManagerConfig } from "./config.js";
Expand Down Expand Up @@ -218,7 +222,7 @@ async function runPackageManager(
stdio,
cwd: directory,
env: {
...process.env,
...getNpmRegistryEnvironment(),
TYPESPEC_CLI_PASSTHROUGH: "1",
},
});
Expand Down
21 changes: 20 additions & 1 deletion packages/compiler/src/package-manger/npm-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,26 @@ const defaultRegistry = `https://registry.npmjs.org`;
* otherwise falls back to the default npm registry.
*/
export function getNpmRegistry(): string {
return (process.env["TYPESPEC_NPM_REGISTRY"] ?? defaultRegistry).replace(/\/$/, "");
const registry = process.env["TYPESPEC_NPM_REGISTRY"]?.trim();
return (registry || defaultRegistry).replace(/\/$/, "");
}

/** Returns the environment used to run npm with the TypeSpec registry override applied. */
export function getNpmRegistryEnvironment(): NodeJS.ProcessEnv {
const environment = { ...process.env };
if (!process.env["TYPESPEC_NPM_REGISTRY"]?.trim()) {
return environment;
}

// Environment variable names are case-insensitive on Windows. Remove any
// existing spelling so Node does not choose between duplicate keys.
for (const name of Object.keys(environment)) {
if (name.toLowerCase() === "npm_config_registry") {
delete environment[name];
}
}
environment["npm_config_registry"] = getNpmRegistry();
return environment;
}

export async function fetchPackageManifest(
Expand Down
37 changes: 36 additions & 1 deletion packages/compiler/test/package-manager/npm-registry.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as http from "http";
import type { AddressInfo } from "net";
import { afterEach, beforeEach, expect, it } from "vitest";
import { fetchPackageManifest } from "../../src/package-manger/npm-registry.js";
import {
fetchPackageManifest,
getNpmRegistry,
getNpmRegistryEnvironment,
} from "../../src/package-manger/npm-registry.js";

let server: http.Server;
let registryUrl: string;
Expand Down Expand Up @@ -34,6 +38,7 @@ beforeEach(async () => {

afterEach(async () => {
delete process.env["TYPESPEC_NPM_REGISTRY"];
delete process.env["NPM_CONFIG_REGISTRY"];
await new Promise<void>((resolve) => server.close(() => resolve()));
});

Expand All @@ -50,3 +55,33 @@ it("strips trailing slash from TYPESPEC_NPM_REGISTRY", async () => {
expect(manifest.name).toBe("test-pkg");
expect(lastRequestUrl).toBe("/test-pkg/1.0.0");
});

it("forwards TYPESPEC_NPM_REGISTRY to npm", () => {
process.env["TYPESPEC_NPM_REGISTRY"] = `${registryUrl}/`;
process.env["NPM_CONFIG_REGISTRY"] = "https://old-registry.example.com";

const environment = getNpmRegistryEnvironment();

expect(environment["npm_config_registry"]).toBe(registryUrl);
expect(environment).not.toHaveProperty("NPM_CONFIG_REGISTRY");
});

it("preserves the npm registry environment when no TypeSpec override is set", () => {
delete process.env["TYPESPEC_NPM_REGISTRY"];
process.env["NPM_CONFIG_REGISTRY"] = "https://configured-registry.example.com";

const environment = getNpmRegistryEnvironment();

expect(environment["NPM_CONFIG_REGISTRY"]).toBe("https://configured-registry.example.com");
});

it.each(["", " "])("does not forward an empty TypeSpec registry override", (registry) => {
process.env["TYPESPEC_NPM_REGISTRY"] = registry;
process.env["NPM_CONFIG_REGISTRY"] = "https://configured-registry.example.com";

const environment = getNpmRegistryEnvironment();

expect(getNpmRegistry()).toBe("https://registry.npmjs.org");
expect(environment["NPM_CONFIG_REGISTRY"]).toBe("https://configured-registry.example.com");
expect(environment).not.toHaveProperty("npm_config_registry");
});
Loading