Skip to content
Closed
Changes from 1 commit
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
Next Next commit
improve julia support
  • Loading branch information
ericphanson committed Jul 21, 2025
commit a7f8efa2ae1164d5fdc06bc19c8cfa5f37bf962d
24 changes: 12 additions & 12 deletions pre_commit/languages/julia.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,36 @@ 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,
require_serial=require_serial,
color=color,
)


def get_env_patch(target_dir: str, version: str) -> PatchesT:
return (
patches = [
('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
def in_env(prefix: Prefix, version: str) -> Generator[None]:
envdir = lang_base.environment_dir(prefix, ENVIRONMENT_DIR, version)
with envcontext(get_env_patch(envdir, version)):
yield


def install_environment(
prefix: Prefix,
version: str,
additional_dependencies: Sequence[str],
) -> 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 +94,11 @@ 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'))

# Julia code to instantiate the hook environment
julia_code = """
@assert length(ARGS) > 0
Expand Down Expand Up @@ -127,6 +127,6 @@ def install_environment(
end
"""
cmd_output_b(
'julia', '-e', julia_code, '--', envdir, *additional_dependencies,
'julia', '--startup-file=no', '-e', julia_code, '--', envdir, *additional_dependencies,
cwd=prefix.prefix_dir,
)