Skip to content

npm run switch Command Fails: Unable to Find OneJS Directory Path #81

@Candlend

Description

@Candlend

Problem Description

When using npm run switch nodejs (or other backends) to switch PuerTS backends, the script fails to locate the OneJS Unity package path, resulting in the following error:

OneJS.Runtime.csproj file does not exist.
Error: The "path" argument must be of type string. Received null

Steps to Reproduce

  1. Install OneJS using Git URL method (in Packages/manifest.json):

    "com.singtaa.onejs": "https://github.com/Singtaa/OneJS.git"
  2. Execute in the App directory:

    npm run switch nodejs
  3. Observe the error output

Error Message

Local .tgz found: tmp\PuerTS_Nodejs_2.2.2.tgz
Extraction completed.
OneJS.Runtime.csproj file does not exist.
Error: The "path" argument must be of type string. Received null

Problem Analysis

The getOneJSUnityDir() function in node_modules/onejs-core/scripts/switch.cjs has the following issues:

  1. Only supports local paths: The script only checks for local paths with file: prefix (line 120), and cannot handle Git URL type package dependencies

  2. Fallback is unreliable: When the path cannot be found from manifest.json, the script attempts to find the OneJS.Runtime.csproj file (line 131), but this file does not exist

  3. Missing error handling: When getOneJSUnityDir() returns null, the subsequent code directly uses path.join(null, ...) causing a crash (line 68)

Relevant Code Locations

  • File: node_modules/onejs-core/scripts/switch.cjs
  • Function: getOneJSUnityDir() (lines 100-176)
  • Call site: Process() function (line 68)
// Line 68 - Missing null check
const onejsDir = await getOneJSUnityDir()
const a = path.join(onejsDir, 'Puerts/Editor/com.tencent.puerts.core.Editor.asmdef')
// ...
// Lines 115-126 - Only supports file: paths
if (v.startsWith("file:")) {
    oneJSPath = path.resolve(projectDir, v.substring(5))
    return oneJSPath
}
// Git URL is ignored
// Lines 129-172 - Fallback searches for csproj file
const csprojPath = path.join(projectDir, 'OneJS.Runtime.csproj')
if (fs.existsSync(csprojPath)) {
    // ...
} else {
    console.error('OneJS.Runtime.csproj file does not exist.')
    return null  // Returns null but caller doesn't handle it
}

Expected Behavior

The script should be able to:

  1. Support Git URL type package dependencies (look for Unity's Library/PackageCache directory)
  2. Provide clearer error messages and usage instructions when the path cannot be found
  3. Gracefully handle errors when getOneJSUnityDir() returns null, instead of crashing

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions