Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .github/workflows/languages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ jobs:
if: matrix.language == 'haskell'
- uses: r-lib/actions/setup-r@v2
if: matrix.os == 'ubuntu-latest' && matrix.language == 'r'

- uses: julia-actions/install-juliaup@v2
if: matrix.language == 'julia'
with:
channel: 'release'
- run: juliaup add 1.10.10
if: matrix.language == 'julia'
- name: install deps
run: python -mpip install -e . -r requirements-dev.txt
- name: run tests
Expand Down
32 changes: 22 additions & 10 deletions pre_commit/languages/julia.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import shutil
from collections.abc import Generator
from collections.abc import Sequence
from typing import Union

from pre_commit import lang_base
from pre_commit.envcontext import _Unset
from pre_commit.envcontext import envcontext
from pre_commit.envcontext import PatchesT
from pre_commit.envcontext import UNSET
from pre_commit.envcontext import Var
from pre_commit.prefix import Prefix
from pre_commit.util import cmd_output_b

Expand Down Expand Up @@ -37,7 +40,7 @@ def run_hook(

cmd = lang_base.hook_cmd(entry, args)
script = cmd[0] if is_local else prefix.path(cmd[0])
cmd = ('julia', script, *cmd[1:])
cmd = ('julia', '--startup-file=no', script, *cmd[1:])
return lang_base.run_xargs(
cmd,
file_args,
Expand All @@ -46,12 +49,17 @@ def run_hook(
)


PatchEntry = tuple[str, Union[str, _Unset, tuple[Union[str, Var], ...]]]
Comment thread
ericphanson marked this conversation as resolved.
Outdated


def get_env_patch(target_dir: str, version: str) -> PatchesT:
return (
patches: list[PatchEntry] = [
('JULIA_LOAD_PATH', target_dir),
# May be set, remove it to not interfer with LOAD_PATH
Comment thread
ericphanson marked this conversation as resolved.
('JULIA_PROJECT', UNSET),
)
]
if version not in ('system', 'default'):
patches.append(('JULIAUP_CHANNEL', version))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't quite right. to properly support language_version pre-commit must install the language itself into the hook environment (and not outside of it such as in the juliaup managed directories)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, pre-commit wants an entire copy of julia installed into the hook environment directory? that's very unusual for julia, which uses content-addressed read-only files and packages to allow reuse across environments. It would also make pre-commit install much slower for julia hooks than it currently is.

return tuple(patches)


@contextlib.contextmanager
Expand All @@ -68,10 +76,6 @@ def install_environment(
) -> None:
envdir = lang_base.environment_dir(prefix, ENVIRONMENT_DIR, version)
with in_env(prefix, version):
# TODO: Support language_version with juliaup similar to rust via
# rustup
# if version != 'system':
# ...

# Copy Project.toml to hook env if it exist
os.makedirs(envdir, exist_ok=True)
Expand Down Expand Up @@ -99,6 +103,14 @@ def install_environment(
shutil.copy(manifest_file, envdir)
break

# copy `src` files if they exist
src_dir = prefix.path('src')
if os.path.isdir(src_dir):
shutil.copytree(
src_dir, os.path.join(envdir, 'src'),
dirs_exist_ok=True,
)

# Julia code to instantiate the hook environment
julia_code = """
@assert length(ARGS) > 0
Expand Down Expand Up @@ -127,6 +139,6 @@ def install_environment(
end
"""
cmd_output_b(
'julia', '-e', julia_code, '--', envdir, *additional_dependencies,
cwd=prefix.prefix_dir,
'julia', '--startup-file=no', '-e', julia_code, '--', envdir,
*additional_dependencies, cwd=prefix.prefix_dir,
)
54 changes: 54 additions & 0 deletions tests/languages/julia_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ def test_julia_hook(tmp_path):
assert run_language(tmp_path, julia, 'src/main.jl') == expected


def test_julia_hook_version(tmp_path):
code = """
using Example
function main()
println("Hello, Julia $(VERSION)!")
end
main()
"""
_make_hook(tmp_path, code)
expected = (0, b'Hello, Julia 1.10.10!\n')
assert run_language(
tmp_path, julia, 'src/main.jl',
version='1.10.10',
) == expected


def test_julia_hook_manifest(tmp_path):
code = """
using Example
Expand Down Expand Up @@ -95,3 +111,41 @@ def test_julia_repo_local(tmp_path):
env_dir, julia, 'local.jl --local-arg1 --local-arg2',
deps=deps, is_local=True,
) == expected


def _make_src_hook(tmp_path, pkg_code, script_code):
# here we have a package with a src dir and a script dir
src_dir = tmp_path.joinpath('src')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feels like from this test this should get handled by the install process. the precedence of copying little bits out of this seems not great and we should find a way to do this step by step

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is in the tests, so we're setting up an example with test files which do get copied as part of the install process

src_dir.mkdir()
src_dir.joinpath('ExamplePkg.jl').write_text(pkg_code)

script_dir = tmp_path.joinpath('scripts')
script_dir.mkdir()
script_dir.joinpath('main.jl').write_text(script_code)

tmp_path.joinpath('Project.toml').write_text(
'name = "ExamplePkg"\n'
'uuid = "df230c44-b485-4b6a-bafb-763c50abe554"\n'
'[deps]\n'
'Example = "7876af07-990d-54b4-ab0e-23690620f79a"\n',
)


def test_julia_hook_src(tmp_path):
pkg_code = """
module ExamplePkg
using Example
export main
function main()
println("Hello, world!")
end
end
"""

script_code = """
using ExamplePkg
main()
"""
_make_src_hook(tmp_path, pkg_code, script_code)
expected = (0, b'Hello, world!\n')
assert run_language(tmp_path, julia, 'scripts/main.jl') == expected