#[SOLVED] Directory.without_file() didn't work as I was expecting it

1 messages · Page 1 of 1 (latest)

limber maple
#

In order to improve the cache mechanism, I've different steps when building my container.

In a previous step, I'm getting my composer.json like below so I can run composer install later on.

self.container = (
    await self.container.
    # [...]
    .with_file(
        "/app/src/composer.json",
        self._stage.source_directory.file("composer.json"),
    )
    .sync()
)

Then I copy my entire codebase but I exclude the composer.json file:

filtered_source: Directory = self.source_directory.without_file(
    "composer.json"
)

self.container = (
    await self.container.terminal()
    .with_mounted_directory("/app/src", filtered_source)
    .terminal()
    # [...]
    .sync()
)

As you can see I'm starting two terminals, before and after the mount.

Before the mount: by doing a ls -al I can see my composer.json (and composer.lock) created earlier. Then I do the mount from my filtered source and, just after, in the second terminal, my composer.json file is no more there.

My expectation was : I exclude the file because I want to keep the file that was already in place.

Am I wrong? Thanks !

#

Here is the output of my first .terminal()statement :

dagger /app/src $ ls -alh
total 232K
drwxr-xr-x 1 root root 4.0K Feb  6 09:58 .
drwxr-xr-x 1 root root 4.0K Feb  6 07:28 ..
-rwxr-xr-x 1 root root 2.6K Feb  6 10:28 composer.json
-rw-r--r-- 1 root root 203K Feb  6 10:28 composer.lock
drwxr-xr-x 1 root root 4.0K Feb  6 10:28 vendor

dagger /app/src $ ls -alh vendor
total 136K
drwxr-xr-x  1 root root 4.0K Feb  6 10:28 .
drwxr-xr-x  1 root root 4.0K Feb  6 09:58 ..
drwxr-xr-x  1 root root 4.0K Feb  6 10:28 bin
drwxr-xr-x  3 root root 4.0K Feb  6 09:58 brick
drwxr-xr-x  3 root root 4.0K Feb  6 09:58 carbonphp
drwxr-xr-x  1 root root 4.0K Feb  6 10:28 composer
drwxr-xr-x  3 root root 4.0K Feb  6 09:58 dflydev
(a lot of folders)

I've two composer files and the vendor folder (what I wish to keep)

And here the output of my second terminal:

dagger /app/src $ ls -alh composer*.*
ls: cannot access 'composer*.*': No such file or directory

dagger /app/src $ ls -alh vendor
total 8.0K
drwxr-xr-x 2 root root 4.0K Feb  6 11:53 .
drwxr-xr-x 1 root root 4.0K Feb  6 11:53 ..

The idea was to exclude composer / vendor from my host and get the one from my previous container.

limber maple
#

Okido, I've found a solution...

I've to keep my vendor folder and composer.json file before. To do this, I use two variables.
Then I use the with_mounted_directory and finay restore my two variables.

# Retrieve the vendor directory from the previous "stage"; same for composer.json
vendor: Directory = self.container.directory("/app/src/vendor")
composer: File = self.container.file("/app/src/composer.json")

# Mount the codebase and the folder with configuration files.
# This will this invalidate the Dagger cache as soon as there is a change
self.container = (
    await self.container.with_mounted_directory("/app/src", filtered_source)
    .with_directory("/app/src/vendor", vendor)
    .with_file("/app/src/composer.json", composer)
    # [...]
    .sync()
)

Perhaps is there a better way to do this but it works like that.

Thank you Dagger dev to have provided this feature.