Skip to content

client: filesystem: write doesn't work with string interpolation #1781

Closed as not planned

Description

@grouville

Context

@Alvise88 is porting his config to Dagger Europa.
His use case is: read a file from the host and modify it using dagger.

Issue

The first implementation doesn't seem to work because no action is being referenced. That way, dagger do doesn't seem to run.

package main

import (
	"dagger.io/dagger"
)

dagger.#Plan & {
	client: filesystem: "test.txt": {
		read: contents:  string
		write: contents: read.contents
	}
}

But, it's not the subject of this issue: what we did is that we created a new action that would use the client filesystem as intermediary steps.

However, when testing, we realized that client: filesystem: "test.txt": write: contents couldn't be inlined in a string:

For all the repro cases below, you need to create a test.txt file with foo written inside

No string interpolation - this works

package main

import(
  "dagger.io/dagger"

  "universe.dagger.io/alpine"
  "universe.dagger.io/docker"
)

dagger.#Plan & {
  client: filesystem: "./test.txt": {
      read: contents: string
      write: contents: actions.hosts.export.contents // works
  }

  actions: hosts: {
    configuration: client.filesystem."/etc/hosts".read.contents

    image: docker.#Build & {
      steps: [
        alpine.#Build & {
          packages: "docker-cli": {}
          packages: "curl": {}
        },
        docker.#Run & {
          always: true
          command: {
            name: "sh"
            flags: "-c": "echo -n bar > /foo.txt"
          }
        },
      ]
    }
    export: dagger.#ReadFile & {
      input: image.output.rootfs
      path:  "foo.txt"
    }
  }
}

With string interpolation - this doesn't work

package main

import(
  "dagger.io/dagger"

  "universe.dagger.io/alpine"
  "universe.dagger.io/docker"
)

dagger.#Plan & {
  client: filesystem: "./test.txt": {
      read: contents: string
      write: contents: "\(actions.hosts.export.contents)" // This doesn't work
  }

  actions: hosts: {
    configuration: client.filesystem."/etc/hosts".read.contents

    image: docker.#Build & {
      steps: [
        alpine.#Build & {
          packages: "docker-cli": {}
          packages: "curl": {}
        },
        docker.#Run & {
          always: true
          command: {
            name: "sh"
            flags: "-c": "echo -n bar > /foo.txt"
          }
        },
      ]
    }
    export: dagger.#ReadFile & {
      input: image.output.rootfs
      path:  "foo.txt"
    }
  }
}

Current solution

  1. Enforce the string type of the client: filesystem: write:
      write: contents: "\(actions.hosts.export.contents)" // This doesn't work

becomes

      write: contents: "\(actions.hosts.export.contents)" // This works now
      write: contents: string

or

      write: contents: "\(actions.hosts.export.contents)" & string // This works now
  1. Do not inline in the client API, but directly in your actions

Potential root causes

@helderco thought this might be related to the dagger do fixes that @aluzzardi and @jlongtine implemented recently.

Activity

  1. helderco commented on Mar 11, 2022

    @helderco
    Contributor

    I was about to create an issue for this. I made a simple test:

    package main
    
    import (
    	"dagger.io/dagger"
    
    	"universe.dagger.io/alpine"
    	"universe.dagger.io/bash"
    )
    
    dagger.#Plan & {
    	client: filesystem: {
    		"./do_works": write: contents: actions.test.contents
    		"./do_works_not": write: contents: "\(actions.test.contents)"
    	}
    
    	actions: {
    		image: alpine.#Build & {
    			packages: bash: {}
    		}
    		build: bash.#Run & {
    			input: image.output
    			script: contents: "echo Hello World! > /output.txt"
    		}
    		// export file seperately for a simpler interpolation reference
    		test: dagger.#ReadFile & {
    			input: build.output.rootfs
    			path:  "output.txt"
    		}
    	}
    }

    This should be added to tests/plan/do. It doesn't have to do with the client.filesystem task per se, it's just that the task runner doesn't pick up the output with the interpolated reference.

  2. jlongtine commented on Mar 11, 2022

    @jlongtine
    Contributor

    So, pretty sure I understand what's happening here.

    If you cue eval @helderco's test, you'll get this disjunction:

            "./do_works_not": {
                write: {
                    contents: "\(actions.test.contents)" & (string | #Secret)
                    $dagger: {
                        task: {}
                    }
                    path: "./do_works_not"
                } | {
                    contents: "\(actions.test.contents)" & #FS
                    $dagger: {
                        task: {}
                    }
                    path: "./do_works_not"
                }
            }

    Because "\(actions.test.contents)" isn't concrete yet. Our first pass of allowing tasks doesn't see it as a task (because it isn't one yet). Thus, it isn't put in the "allow" list, even though it's dependent on the action we are running. So... it's then disallowed from running when "\(actions.test.contents)" becomes concrete, the disjunction resolves, and it becomes a task we can run.

  3. helderco commented on Mar 31, 2022

    @helderco
    Contributor

    Related to cue-lang/cue#629, tracked in #493

  4. helderco commented on Apr 10, 2022

    @helderco
    Contributor

    There's an easy and more appropriate solution:

    -write: contents: "\(actions.hosts.export.contents)"
    +write: contents: actions.hosts.export.contents

    As for the unexpected behavior of the interpolation, I'll defer to the #493 issue.

  5. helderco commented on Apr 11, 2022

    @helderco
    Contributor

    On second thought, the usage of interpolation here shouldn't fail on plan load. Intuitively it should get resolved as a string, but it isn't. We should drill down the repro into pure CUE and see if it's really about cue-lang/cue#629 or something else.

  6. added
    kind/bug
    on Jul 13, 2022
  7. grouville commented on Jan 5, 2023

    @grouville
    MemberAuthor

    Closing, as Cue API is not a priority for now

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