Skip to content
Merged
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
4 changes: 4 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export function detectCrossCompilation(
return true
}
if (
config.os === undefined &&
process.env.npm_config_target_os !== undefined &&
platforms.has(process.env.npm_config_target_os as NodeJS.Platform) &&
process.env.npm_config_target_os !== process.platform
Expand All @@ -140,9 +141,11 @@ export function detectCrossCompilation(
logger.debug(
`Cross compilation detected: npm_config_target_os (${process.env.npm_config_target_os}) differs from process.platform (${process.platform})`,
)
config.os = process.env.npm_config_target_os as NodeJS.Platform
return true
}
if (
config.arch === undefined &&
process.env.npm_config_target_arch !== undefined &&
architectures.has(process.env.npm_config_target_arch as NodeJS.Architecture) &&
process.env.npm_config_target_arch !== process.arch
Expand All @@ -151,6 +154,7 @@ export function detectCrossCompilation(
logger.debug(
`Cross compilation detected: npm_config_target_arch (${process.env.npm_config_target_arch}) differs from process.arch (${process.arch})`,
)
config.arch = process.env.npm_config_target_arch as NodeJS.Architecture
return true
}
return false
Expand Down
27 changes: 25 additions & 2 deletions test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ suite("Config Functions", () => {
expect(result.buildType).toBe("Release")
})

test("should handle cross compilation flags", async () => {
test("should detect os cross compilation", async () => {
const partialConfig: Partial<BuildConfiguration> = {
os: process.platform === "win32" ? "linux" : "win32",
arch: "x64",
Expand All @@ -142,6 +142,7 @@ suite("Config Functions", () => {
const result = await getBuildConfig(mockBuildOptions, partialConfig, mockConfigFile)

expect(result.cross).toBe(true)
expect(result.os).toBe(process.platform === "win32" ? "linux" : "win32")
})

test("should respect npm_config_target_arch when it matches config.arch", async () => {
Expand Down Expand Up @@ -195,7 +196,7 @@ suite("Config Functions", () => {
vi.restoreAllMocks()
})

test("should respect npm_config_target_arch when it differs from process.arch", async () => {
test("should respect config.arch when it differs from process.arch even if npm_config_target_arch is set", async () => {
// Set npm_config_target_arch to a different architecture than the current one
process.env.npm_config_target_arch = process.arch === "x64" ? "arm64" : "x64"

Expand All @@ -206,7 +207,29 @@ suite("Config Functions", () => {

const result = await getBuildConfig(mockBuildOptions, partialConfig, mockConfigFile)

expect(result.cross).toBe(false)
expect(result.arch).toBe(process.arch)
})

test("should respect npm_config_target_arch when it differs from process.arch with default config", async () => {
// Set npm_config_target_arch to a different architecture than the current one
process.env.npm_config_target_arch = process.arch === "x64" ? "arm64" : "x64"

const result = await getBuildConfig(mockBuildOptions, {}, mockConfigFile)

expect(result.cross).toBe(true)
expect(result.arch).toBe(process.env.npm_config_target_arch)
expect(result.os).toBe(process.platform)
})

test("should respect npm_config_target_os when it differs from process.platform with default config", async () => {
// Set npm_config_target_os to a different platform than the current one
process.env.npm_config_target_os = process.platform === "win32" ? "linux" : "win32"

const result = await getBuildConfig(mockBuildOptions, {}, mockConfigFile)

expect(result.cross).toBe(true)
expect(result.os).toBe(process.env.npm_config_target_os)
expect(result.arch).toBe(process.arch)
})

Expand Down
Loading