Skip to content

🐞 Node.js SDK - ClientError: runtime error: invalid memory address or nil pointer dereference #4130

Closed

Description

@slumbering

What is the issue?

I'm trying to write a new file with the following script:

KO

import Client, { connect } from "@dagger.io/dagger"

connect(async (client: Client) => {
    const newFile = await client
       .container()
       .directory("/").entries()
})

OK

import Client, { connect } from "@dagger.io/dagger"

connect(async (client: Client) => {
    const newFile = await client
       .container()
       .from("alpine")
       .directory("/").entries()
})

Log output

(node:41070) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
file:///private/tmp/my-app/node_modules/@dagger.io/dagger/dist/api/utils.js:5
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
                                                                ^
GraphQLRequestError: Error message
    at file:///private/tmp/my-app/node_modules/@dagger.io/dagger/dist/api/utils.js:119:23
    at Generator.throw (<anonymous>)
    at rejected (file:///private/tmp/my-app/node_modules/@dagger.io/dagger/dist/api/utils.js:5:65)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  cause: ClientError: runtime error: invalid memory address or nil pointer dereference: {"response":{"data":null,"errors":[{"message":"r
untime error: invalid memory address or nil pointer dereference","locations":[{"line":5,"column":9}],"path":["container","directory"]}],"status":200,"headers":{}},"request":{"query":"\n        {\n        container  {\n      \n        directory (path: \"/\") {\n      \n        withNewFile (path: \"myfile\",contents: \"This string has quotes \\\"\") {\n      \n        file (path: \"myfile\") {\n      \n        id  }}}}\n      }\n      "}}                                                                                                              at /private/tmp/my-app/node_modules/graphql-request/src/index.ts:497:11
      at step (/private/tmp/my-app/node_modules/graphql-request/dist/index.js:63:23)
      at Object.next (/private/tmp/my-app/node_modules/graphql-request/dist/index.js:44:53)
      at fulfilled (/private/tmp/my-app/node_modules/graphql-request/dist/index.js:35:58)
      at processTicksAndRejections (node:internal/process/task_queues:96:5) {
    response: { data: null, errors: [Array], status: 200, headers: [Headers] },
    request: {
      query: '\n' +
        '        {\n' +
        '        container  {\n' +
        '      \n' +
        '        directory (path: "/") {\n' +
        '      \n' +
        '        withNewFile (path: "myfile",contents: "This string has quotes \\"") {\n' +
        '      \n' +
        '        file (path: "myfile") {\n' +
        '      \n' +
        '        id  }}}}\n' +
        '      }\n' +
        '      ',
      variables: undefined
    }
  },
  code: 'D100',
  requestContext: {
    query: '\n' +
      '        {\n' +
      '        container  {\n' +
      '      \n' +
      '        directory (path: "/") {\n' +
      '      \n' +
      '        withNewFile (path: "myfile",contents: "This string has quotes \\"") {\n' +
      '      \n' +
      '        file (path: "myfile") {\n' +
      '      \n' +
      '        id  }}}}\n' +
      '      }\n' +
      '      ',
    variables: undefined
  },
  response: {
    data: null,
    errors: [ [Object] ],
    status: 200,
    headers: Headers { [Symbol(map)]: [Object: null prototype] }
  }
}

Steps to reproduce

1 - Follow the getting started steps 1 and 2 to install Dagger SDK:

2 - Create a new file .ts (eg: build.ts) in your root dir

3 - Copy paste the following script:

import Client, { connect } from "@dagger.io/dagger"

connect(async (client: Client) => {

  const stringWithQuotes = "This string has quotes \""
  const newFile = await client
       .container()
       .directory("/")
       .withNewFile("myfile", stringWithQuotes)
       .file("myfile").id()
})

4 - Run this command (if necessary change build.ts with your script name):

node --loader ts-node/esm ./build.ts

Dagger version

dagger devel (c3124a9) darwin/arm64

OS version

macOs 13.0.1 (22A400)

Activity

  1. added
    kind/bug
    on Dec 8, 2022
  2. slumbering commented on Dec 8, 2022

    @slumbering
    ContributorAuthor
  3. slumbering commented on Dec 22, 2022

    @slumbering
    ContributorAuthor

    Did you get the chance to tackle this issue @sipsma @aluzzardi ? Or maybe I'm doing something wrong in the example above ? Thanks

  4. slumbering commented on Jan 10, 2023

    @slumbering
    ContributorAuthor

    👋 @sipsma @aluzzardi . Adding .from('alpine') removes the error.

  5. slumbering commented on Jan 10, 2023

    @slumbering
    ContributorAuthor

    Same error with go:

        test, err := client.Container().Directory("/").Entries(ctx)
    
        fmt.Println(test)
    
        if err != nil {
            return err
        }
        ...
    Building with Dagger
    []
    input:1: container.directory runtime error: invalid memory address or nil pointer dereference
    
    Please visit https://dagger.io/help#go for troubleshooting guidance.
  6. aluzzardi commented on Jan 10, 2023

    @aluzzardi
    Contributor

    Looks like a bug deep down, not converting a nil pointer into an llb.Scratch

    @vito Would you know if there's one magic spot where we could handle that?

  7. vito commented on Jan 10, 2023

    @vito
    Contributor

    @aluzzardi This error has bubbled up from Buildkit even when dealing with llb.Scratch in various places. I know @sipsma tracked down at least one of them. I haven't found a silver bullet, it feels like playing wack-a-mole. 😕

    I tracked this one down to here:

    dagger/core/directory.go

    Lines 129 to 134 in e88a409

    stat, err := ref.StatFile(ctx, bkgw.StatRequest{
    Path: src,
    })
    if err != nil {
    return nil, err
    }

    Above it, we already try to handle scratch by special-casing it and setting payload.LLB to nil and checking for it, but it ended up being set to a marshalled llb.Scratch() instead which is what causes panics.

    dagger/core/directory.go

    Lines 107 to 115 in e88a409

    // empty directory, i.e. llb.Scratch()
    if payload.LLB == nil {
    if path.Clean(src) == "." {
    // fake out a reasonable response
    return &fstypes.Stat{Path: src}, nil
    }
    return nil, fmt.Errorf("%s: no such file or directory", src)
    }

    We ended up with a marshaled llb.Scratch() instead of nil because locatePath called NewDirectory (init below) with llb.Scratch() (returned by FSState):

    dagger/core/container.go

    Lines 681 to 690 in e88a409

    // Not found in a mount
    st, err := payload.FSState()
    if err != nil {
    return nil, nil, err
    }
    found, err = init(ctx, st, containerPath, payload.Platform)
    if err != nil {
    return nil, nil, err
    }

    One way to handle this could be to change NewDirectory to take a *llb.State to support creating a directory with LLB: nil, but that'll make calls like NewDirectory(ctx, llb.Image(...), ...) pretty ugly.

    We can also change NewDirectory to check for llb.Scratch() with st.Output() != nil but I remember there being some situation where that didn't work. 😮‍💨

    Long story short this is probably best fixed in upstream Buildkit. It's weird that llb.Scratch(), a supported API, leads to nil panics in any situation.

  8. vito commented on Feb 13, 2023

    @vito
    Contributor

    Closing optimistically as #4248 (comment) included a fix for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions