Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Yet another 126 error but only when testing with jest #633

Closed
mathiasbockwoldt opened this issue Jan 4, 2023 · 2 comments
Closed

Yet another 126 error but only when testing with jest #633

mathiasbockwoldt opened this issue Jan 4, 2023 · 2 comments

Comments

@mathiasbockwoldt
Copy link

Not sure if this belongs here or rather in the jest issue tracker.

I wrote a package that uses native .dll/.so files. The including code looks like this:

import * as ffi from 'ffi-napi';
import * as ref from 'ref-napi';
import path from 'node:path';

const pathToDll ='./lib/win/mylib.dll';
const pathToSo = './lib/linux/mylib.so';
const pathToLib = process.platform === 'win32' ? pathToDll : pathToSo;

if (process.platform === 'win32') {
  const kernel32 = ffi.Library('kernel32', {
    SetDllDirectoryA: [ref.types.bool, [ref.types.CString]],
  });
  const dllDir = path.dirname(path.resolve(pathToDll));
  kernel32.SetDllDirectoryA(dllDir);
  const libPath = path.resolve('.');                                   // MARK
  process.env.PATH = `${process.env.PATH}${path.delimiter}${libPath}`; // MARK
}

export const mylib = ffi.Library(pathToLib, {
  testMethod: [ref.types.int, [ref.types.int]]
}

Under Linux, it works nicely. Under Windows it works if called directly from node or in a script, but if I call this using jest, I get Dynamic Linking Error: Win32 error 126.

There are two things here that puzzle me.

  1. Why do I have to include the root path to the PATH env variable (lines marked with MARK)? If I delete these lines, the Windows version will create the 126 error also when calling the script directly. I don't understand this, because the dlls are in the ./lib/win directory and that is included via SetDllDirectory anyway.
  2. The jest version works if I add the root folder of the script to PATH manually before I start jest. Either way (manually or by the lines marked with MARK), I can see that the root folder is in the PATH (via console.log), but it only works if I add it manually before starting jest.
@mathiasbockwoldt
Copy link
Author

Related: jestjs/jest#13730

@mathiasbockwoldt
Copy link
Author

Sorry, folks, my error.
The problem was not the path variable, but the relative path to the dll (const pathToDll ='./lib/win/mylib.dll';). That needs to become an absolute path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant