Skip to content

Failed to link native package on Windows: collect_modules entered !import.is_native #792

@Alex6357

Description

@Alex6357

What happened

When compiling a file that imports a native package, collect_modules prints Warning: Could not resolve import 'native' from main.ts and Perry fails to link the executable.

What you expected

Perry resolves the native module correctly.

Minimal reproduction

  1. Create a native package named native using perry native init native
  2. Create a new package in the same folder and install the native dependency using pnpm add ../native
  3. Add main.ts:
// main.ts
import { hello } from "native";
console.log(hello());
  1. Compile main.ts using perry compile ./main.ts

Environment

  • Perry version: 0.5.891
  • Host OS: Windows 11
  • Target: native
  • Rust toolchain: 1.94.1
  • Installed via: winget

Diagnostic output

PS D:\Develop\.test\volt\caller> perry compile ./main.ts
Collecting modules...
  Warning: Could not resolve import 'native' from main.ts
Found 1 module(s): 1 native, 0 JavaScript
Generating code...
Wrote object file: main_ts.o
Linking (runtime-only)...
main_ts.o : error LNK2019: 无法解析的外部符号 hello,函数 main 中引用了该符号
main.exe : fatal error LNK1120: 1 个无法解析的外部命令
Error: Linking failed

Anything else

Adding "types": "src/main.ts" or "main": "src/main.ts" to the native package's package.json allows Perry to compile successfully:

PS D:\Develop\.test\volt\caller> perry compile ./main.ts
Collecting modules...
  Native library: native (1 FFI functions)
Found 2 module(s): 2 native, 0 JavaScript
Generating code...
Wrote object file: caller_main_ts.o
Wrote object file: native_src_index_ts.o
Linking (runtime-only)...
Building native library: native ...
    Finished `release` profile [optimized] target(s) in 0.17s
Wrote executable: main.exe
Binary size: 1.1MB

However, hello() then falls back to its TypeScript implementation:

PS D:\Develop\.test\volt\caller> .\main.exe              
Hello, undefined!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions