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

Does not work in Docker container based on node:slim #1986

Open
kyoheiu opened this issue Sep 1, 2023 · 2 comments
Open

Does not work in Docker container based on node:slim #1986

kyoheiu opened this issue Sep 1, 2023 · 2 comments

Comments

@kyoheiu
Copy link

kyoheiu commented Sep 1, 2023

System information

These are about the container.

  • node version: ENV NODE_VERSION=20.5.1
  • npm or yarn version: 9.8.1
  • OS/version/architecture: debian12
  • Applicable nodegit version: 0.27.0

Trying to use nodegit with SvelteKit.
With e.g. the following function, dev build works perfectly fine.

export const addAndCommit = async (file: string, message: string) => {
	console.log(Git.version);
	const repo = await Git.Repository.open(DATA_PATH);
	const index = await repo.refreshIndex();
	const result = await index.addByPath(file);
	if (result === 0) {
		throw Error(`Cannot add to index: ${file}`);
	}
	await index.write();

	const changes = await index.writeTree();
	const head = await Git.Reference.nameToId(repo, 'HEAD');
	const parent = await repo.getCommit(head);
	const author = generateAuthor();
	await repo.createCommit('HEAD', author, author, message, changes, [parent]);
};

For production I containerized with this Dockerfile -

FROM node:slim

WORKDIR /app

COPY . .
RUN RUN apt-get install -y python3 libkrb5-dev gcc openssl libssh2-1-dev g++ make
RUN npm ci
RUN npm run build

ENV NODE_ENV=production
EXPOSE 3000
CMD ["node", "build/index.js"]

and all commands succeeded, including npm ci. But cannot read the Git repository, receiving this error message.

"err":{"type":"TypeError","message":"Cannot read properties of undefined (reading 'open')","stack":"TypeError: Cannot read properties of undefined (reading 'open')\n    at addAndCommit (file:///carbon/build/server/chunks/git-7c944c50.js:6:37)\n    at POST (file:///carbon/build/server/chunks/_server.ts-8d43896a.js:47:15)\n    at async render_endpoint (file:///carbon/build/server/index.js:1570:20)\n    at async resolve (file:///carbon/build/server/index.js:3942:22)\n    at async respond (file:///carbon/build/server/index.js:3831:22)\n    at async Array.ssr (file:///carbon/build/handler.js:1221:3)"}

I may miss some packages to make nodegit work inside docker container, but since we do not have any official documentation about including this library in the container I'm not sure.

Does anyone make it in a docker container?

@weedz
Copy link
Contributor

weedz commented Oct 3, 2023

Seems to work with the latest alpha version (0.28.0-alpha.21).

Tested with this Dockerfile:

FROM node:slim

WORKDIR /app

COPY . .
RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y python3 libkrb5-dev gcc openssl libssh2-1-dev g++ make
RUN JOBS=max npm ci

ENV NODE_ENV=production
CMD ["node", "main.js"]

(This installs with node v20.8.0 in the container.)

main.js:

const Git = require("nodegit");

(async () => {
    const repo = await Git.Repository.open("/path/to/git/repo");

    const index = await repo.refreshIndex();

    const status = await repo.getStatusExt();

    console.log("Repo status:", status);
})();

package.json:

{
  "name": "nodegit-test",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "podman:build": "podman build . --tag nodegit-test",
    "podman:run": "podman run -t nodegit-test"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "nodegit": "0.28.0-alpha.21"
  }
}

When executed in the docker container this throws an exception since the repository we try to open does not exist in the container:

[Error: failed to resolve path '/path/to/git/repo': No such file or directory] {
  errno: -3,
  errorFunction: 'Repository.open'
}

But just tried with [email protected] and seems to work just fine, same error Error: failed to resolve path ....

Also did a quick test with ESM modules (setting "type": "module" in package.json and changing the import to import Git from "nodegit";). This also seems to work fine.

Could it be different node versions on the host and container resulting in slightly different module resolutions or something similar? Or try re-downloading the container (think it is docker build --no-cache --pull) and add RUN apt-get update && apt-get upgrade -y to the Dockerfile.

I vaguely recall some similar behavior on MacOS (maybe?) where some functions where missing on the "Git" object (nodegit import). That might have been something with nodegit being built against a different version of openssl resulting in some things working and others didn't. But don̈́t quite remember..

Don't know if it can help somehow but if you execute

console.dir(Git.__proto__, { depth: 1, showHidden: true, showProxy: true });

it should show:

Repository: [Function: Repository] {
...
}

somewhere in the output.

@weedz
Copy link
Contributor

weedz commented Oct 3, 2023

Also just noticed nodegit can't be imported as

import * as Git from "nodegit";

Then you get all the exports and functions on Git.default. So Git.Repository.open() becomes Git.default.Repository.open().

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

2 participants