#Issues creating a file using withNewFile

1 messages · Page 1 of 1 (latest)

stuck robin
#

I am building a Dagger module leveraging AI through LangChain to generate unit tests for a Sveltekit project. As part of this, I want to create a “tests” folder and a file “Home.test.ts” within the tests folder.

Currently, I am creating a tests folder at the root level of the working directory.
const tempDir = await root.withNewDirectory("/tests");

I attempt to create a new file within the tests folder:
const testFile = await root.withNewFile("/tests/Home.test.ts", "")

I mount this directory:
.withMountedDirectory("/mnt", tempDir)

However when I inspect the structure of the directory I don’t see the Home.test.ts file;
✔ exec tree /mnt 0.9s
┃ ├── routes
┃ │ ├── +page.svelte
┃ ├── setupTests.ts
┃ └── tests

I’ve tried a few things;

  1. I noticed tests is read only, I tried to change permissions to make it read and write before creating the new file within this folder:
    "chmod", "775", "/mnt/tests"]
  2. I tried creating the file within routes without creating a tests folder
  3. I tried running using shell scripts to create a directory and then create a file within the directory

As a side question:
I noticed that even when I mount my working directory to the Dagger container, the changes I make within Dagger are only within the container and not synchronized locally. After reading one of your GitHub issues answer I realized WithMountedDirectory isn’t bind mounting the files in my machine to the build
link:https://github.com/dagger/dagger/discussions/6688

I had initially built this module outside of Dagger to create the files and folders for my tests in my directory. I assume if I cannot replicate this behavior with Dagger, I could create a Docker image of the container. Any thoughts on this?

GitHub

I'm having local dagger v0.9.10 (registry.dagger.io/engine) darwin/arm64 and having mounted directory using withMountedDirectory and expected that changes to that directory are reflected in hos...

jolly siren
#

For your main question, it looks like you probably wanted to mount testFile and not testDir here .withMountedDirectory("/mnt", tempDir). Side note, I don't believe any of those operations needed await

stuck robin
#

I used the export method to write ‘the contents of the directory to a path on the host’. I assume this means the directory should be visible locally in the path defined in exportPath, yet the file isn’t available. Am I misunderstanding what export does?

` const tempDir = root.withNewDirectory("/tests");
const testFile = tempDir.withNewFile("/tests/Home.test.ts", "")

// Mounting directory to container
const container = dag.container()
.from("alpine:latest")
.withMountedDirectory("/mnt", testFile)
.withWorkdir("/mnt")

 const finalPath = "/Users/EmmS21/Desktop/PersonalWork"
 return this.exportUpdatedDirectory(tempDir, finalPath);

@func()
async exportUpdatedDirectory(updatedDir: Directory, exportPath: string): Promise<string> {
const success = await updatedDir.export(exportPath);
if (success) {
let successMessage = Successfully exported directory to ${exportPath};
return successMessage;
} else {
throw new Error(Failed to export directory to ${exportPath});
}
}`

jolly siren
#

Kind of! See https://docs.dagger.io/manuals/user/620164/host-fs
When you call export inside your module, you're exporting to the module's container, which is not your host. What you want to do is export from the function call.

So if your function returns a Directory, and you call something like
dagger call generate-tests <args>, you would instead call
dagger call generate-tests <args> export --path . or
dagger call generate-tests <args> --output .

Dagger Functions do not have access to the filesystem of the host you invoke the Dagger Function from (i.e. the host you execute a CLI command like dagger call from). Instead, host files and directories need to be explicitly passed when executing dagger call.

stuck robin
#

Out of curiosity what are the barriers to enabling the container to interact directly with the host?

spice relic