microsoft / node-pty

Fork pseudoterminals in Node.JS
Other
1.43k stars 232 forks source link

"Error: chdir() failed: Not a directory" when in Docker on M1 using DOCKER_DEFAULT_PLATFORM=linux/amd64 #545

Closed michalc closed 1 year ago

michalc commented 2 years ago

Environment details

Issue description

When trying to run in Docker on an Apple M1 using the environment variable DOCKER_DEFAULT_PLATFORM=linux/amd64 I get the error: Error: chdir() failed: Not a directory

A hopefully minimal example:

Dockerfile

FROM debian:bullseye-20220527

RUN \
    apt-get update && \
    apt-get install -y \
        build-essential \
        curl && \
    curl -sL https://deb.nodesource.com/setup_14.x | bash - && \
    apt-get install -y nodejs && \
    rm /etc/apt/sources.list.d/nodesource.list && \
    rm -rf /var/lib/apt/lists/*

RUN \
    npm install node-pty@0.11.0-beta19

COPY n.js /

n.js (the example from the README):

var os = require('os');
var pty = require('node-pty');

var shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';

var ptyProcess = pty.spawn(shell, [], {
  name: 'xterm-color',
  cols: 80,
  rows: 30,
  cwd: process.env.HOME,
  env: process.env
});

ptyProcess.on('data', function(data) {
  process.stdout.write(data);
});

ptyProcess.write('ls\r');
ptyProcess.resize(100, 40);
ptyProcess.write('ls\r');

Then I build the Dockerfile using

 DOCKER_DEFAULT_PLATFORM=linux/amd64 docker build . -t n

and then running it using

DOCKER_DEFAULT_PLATFORM=linux/amd64 docker run --rm -it n node n.js

Gives the error:

/node_modules/node-pty/lib/unixTerminal.js:103
        var term = pty.fork(file, args, parsedEnv, cwd, _this._cols, _this._rows, uid, gid, (encoding === 'utf8'), closeFDs, onexit, helperPath);
                       ^

Error: chdir() failed: Not a directory
    at new UnixTerminal (/node_modules/node-pty/lib/unixTerminal.js:103:24)
    at Object.spawn (/node_modules/node-pty/lib/index.js:29:12)
    at Object.<anonymous> (/n.js:6:22)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
    at internal/main/run_main_module.js:17:47

If I don't use DOCKER_DEFAULT_PLATFORM=linux/amd64 then it seems to work fine

Tyriar commented 1 year ago

What's $HOME equal to? It's failing when changing the directory to cwd in options.

michalc commented 1 year ago

Adding in

console.log('HOME', process.env.HOME);

into n.js shows that it's /root both with and without DOCKER_DEFAULT_PLATFORM=linux/amd64

(However, I now cannot reproduce the issue)

Tyriar commented 1 year ago

Strange 🤷