Skip to content

Commit

Permalink
configure yarn request output env variables
Browse files Browse the repository at this point in the history
Several yarn settings must be supplied by the cachi2 request output in order
for the user to make use of the prefetched dependencies. These will be supplied
by way of environment variables, which when sourced, will override any
preexisting user configuration in .yarnrc.yml

These yarn settings are all related to the process of installing cachi2-fetched
dependencies from the globalFolder. Cachi2 must populate globalFolder for the
build rather than cacheFolder because when mounting globalFolder, the
`yarn install` step in the Containerfile will copy everything from the global
cache to the project-local cache. That means the project-local cache stays
populated after the build.

When mounting cacheFolder, `yarn install` doesn't do anything (the cache
is already populated). But when the build ends, podman unmounts the
folder and the cache disappears. When you try to run the app:

    Error: Required package missing from disk.
    If you keep your packages inside your repository then restarting the Node process may be enough.
    Otherwise, try to run an install first.

YARN_GLOBAL_FOLDER must be configured to the deps/yarn
subdirectory of the cachi2 request output directory. This
is the path that cachi2 will populate with the prefetched
dependencies.

YARN_ENABLE_MIRROR must be set to true, otherwise the user
build will not make use of the global cache located at
the globalFolder path that cachi2 prepopulates.

YARN_ENABLE_GLOBAL_CACHE must be set to false, otherwise the
user build will exclusively rely on the cachi2 populated
globalFolder cache rather than copying the dependencies to
cacheFolder. If the volume containing globalFolder is only mounted
at build-time and the dependencies are not copied to cacheFolder,
the dependencies will be missing at run-time.

YARN_ENABLE_IMMUTABLE_CACHE must be set to false, otherwise
the `yarn install` will fail to populate cacheFolder from the
cachi2-supplied globalFolder cache.

Signed-off-by: Taylor Madore <[email protected]>
  • Loading branch information
taylormadore authored and eskultety committed Nov 29, 2023
1 parent fbca8eb commit 6e3711c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
9 changes: 8 additions & 1 deletion cachi2/core/package_managers/yarn/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,11 @@ def _undo_changes(project: Project) -> None:

def _generate_environment_variables() -> list[EnvironmentVariable]:
"""Generate environment variables that will be used for building the project."""
return []
env_vars = {
"YARN_ENABLE_GLOBAL_CACHE": {"value": "false", "kind": "literal"},
"YARN_ENABLE_IMMUTABLE_CACHE": {"value": "false", "kind": "literal"},
"YARN_ENABLE_MIRROR": {"value": "true", "kind": "literal"},
"YARN_GLOBAL_FOLDER": {"value": "deps/yarn", "kind": "path"},
}

return [EnvironmentVariable(name=name, **obj) for name, obj in env_vars.items()]
17 changes: 17 additions & 0 deletions tests/unit/package_managers/yarn/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,28 @@
import semver

from cachi2.core.errors import PackageRejected, UnexpectedFormat, YarnCommandError
from cachi2.core.models.output import EnvironmentVariable
from cachi2.core.package_managers.yarn.main import (
_configure_yarn_version,
_fetch_dependencies,
_generate_environment_variables,
_resolve_yarn_project,
_set_yarnrc_configuration,
)
from cachi2.core.package_managers.yarn.project import YarnRc
from cachi2.core.rooted_path import RootedPath


@pytest.fixture()
def yarn_env_variables() -> list[EnvironmentVariable]:
return [
EnvironmentVariable(name="YARN_ENABLE_GLOBAL_CACHE", value="false", kind="literal"),
EnvironmentVariable(name="YARN_ENABLE_IMMUTABLE_CACHE", value="false", kind="literal"),
EnvironmentVariable(name="YARN_ENABLE_MIRROR", value="true", kind="literal"),
EnvironmentVariable(name="YARN_GLOBAL_FOLDER", value="deps/yarn", kind="path"),
]


class YarnVersions(Enum):
YARN_V1 = semver.VersionInfo(1, 0, 0)
YARN_V2 = semver.VersionInfo(2, 0, 0)
Expand Down Expand Up @@ -212,3 +224,8 @@ def test_set_yarnrc_configuration(mock_write: mock.Mock) -> None:

assert yarn_rc._data == expected_data
assert mock_write.called_once()


def test_generate_environment_variables(yarn_env_variables: list[EnvironmentVariable]) -> None:
result = _generate_environment_variables()
assert result == yarn_env_variables

0 comments on commit 6e3711c

Please sign in to comment.