Skip to content

For the x86 CPU os.machine() returns i686 instead of i386 #58493

@RareScrap

Description

@RareScrap

Version

22.16.0

Platform

Microsoft Windows NT 10.0.19045.0 x86

Subsystem

os

What steps will reproduce the bug?

  1. Run os.machine() in node x86 REPL

How often does it reproduce? Is there a required condition?

100% reproducible

What is the expected behavior? Why is that the expected behavior?

The os.machine() returns i386

What do you see instead?

The os.machine() returns i686 which indicates the x86 app running on x64 CPU (which makes no sense for a x86 CPU host)

Additional information

The main problem - there's no way to determine the true Windows host OS architecture in NodeJS (process.arch is not about it)

Workaround

You can determine the true Windows host OS architecture using the PROCESSOR_ARCHITECTURE environment variable:

function getWindowsOsArchitectureUsingPlatform(): NodeJS.Architecture | undefined {
    const osArchitectureEnvVar = this.getWindowsOsArchitectureEnvVar()
    const sanitizedOsArchitectureEnvVar = osArchitectureEnvVar?.toLowerCase()
    switch (sanitizedOsArchitectureEnvVar) {
        // Values listed in win32 docs:
        // https://learn.microsoft.com/en-us/windows/win32/winprog64/wow64-implementation-details#environment-variables
        case "amd64": return "x64"
        case "ia64": return "x64"
        case "arm64": return "arm64"
        case "x86": return "ia32"
    }

    return undefined
}

function getWindowsOsArchitectureEnvVar(): string | undefined {
    // Despite its name, the PROCESSOR_ARCHITECTURE environment variable stores the architecture of the operating
    // system, not the CPU itself (e.g. for Windows on x86 on an x86_64 processor, the variable has the value `x86`)
    const osArchitectureEnvVar = process.env["PROCESSOR_ARCHITECTURE"]
    const sanitizedOsArchitectureEnvVar = osArchitectureEnvVar?.toLowerCase()
    // When running a 32-bit process (x86) on a 64-bit operating system (x86_64, arm64), the actual
    // value of PROCESSOR_ARCHITECTURE are contained in PROCESSOR_ARCHITEW6432
    // https://learn.microsoft.com/en-us/windows/win32/winprog64/wow64-implementation-details#environment-variables
    const is32BitProcess = sanitizedOsArchitectureEnvVar === "x86"
    if (is32BitProcess) {
        const wowCpuArchitectureEnvVar = process.env["PROCESSOR_ARCHITEW6432"]
        if (wowCpuArchitectureEnvVar) return wowCpuArchitectureEnvVar
    }

    return osArchitectureEnvVar
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    v22.xIssues that can be reproduced on v22.x or PRs targeting the v22.x-staging branch.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions