Skip to content

Conversation

@allansp84
Copy link
Contributor

Pull Request Description

This PR resolves build failures when copying files to /tmp or /var/tmp using the copy primitive on Singularity / Apptainer 3.6 and later by introducing a selective %setup fallback, enabled by default.

The change aligns HPCCM behavior with upstream Singularity restrictions while preserving backward compatibility and minimizing user-facing disruption.

Background

Singularity 3.6+ no longer allows files to be staged into /tmp or /var/tmp via the %files section, as the container root filesystem does not yet exist at that stage. This caused HPCCM recipes using:

copy(src='foo', dest='/tmp/foo')

to fail with a runtime error (see #345).

While --working-directory addresses build-time execution in %post, it does not affect %files staging and therefore cannot resolve this issue.

What this PR does:

  1. Automatic %setup fallback (default behavior)

    When targeting Singularity 3.6+:

    Only copy entries whose destination is under /tmp or /var/tmp
    are moved into a %setup block.

    Other copy entries remain in %files, preserving existing semantics.

    Example output:

    %setup
        mkdir -p ${SINGULARITY_ROOTFS}/tmp
        cp -a foo ${SINGULARITY_ROOTFS}/tmp/foo
    
    %files
        bar /opt/bar
  2. Configurable behavior

    A new configuration flag is introduced:

    hpccm.config.set_singularity_tmp_fallback(True)  # default

    and can be disabled via CLI:

    --no-singularity-tmp-fallback

    If disabled, HPCCM preserves strict behavior and raises a clear runtime
    error on Singularity 3.6+ when /tmp or /var/tmp appears in %files.

References

#345
apptainer/singularity#5848
apptainer/apptainer#490

Author Checklist

  • Updated documentation (pydocmd generate) if any docstrings have been modified
  • Passes all unit tests

Singularity 3.6 and later disallow staging files to /tmp or /var/tmp
via the %files section before the container rootfs exists.

Add a selective %setup fallback that copies only the affected entries
while preserving existing %files behavior for all other paths.

This resolves staging failures introduced by Singularity 3.6 without
impacting Docker or multi-stage builds.
Introduce a global configuration flag to enable or disable the
automatic %setup fallback for /tmp and /var/tmp destinations on
Singularity >= 3.6.

The fallback is enabled by default to preserve backward compatibility.
Expose the Singularity tmp fallback configuration via the command-line
interface using a standard argparse boolean flag.

This allows users to restore the pre-3.6 error behavior when staging
files to /tmp or /var/tmp via %files.
Add unit tests covering:
- single and multiple /tmp and /var/tmp destinations
- nested paths
- mixed safe and tmp destinations
- disabled fallback error behavior
- unchanged Docker behavior
- interaction with _exclude_from
Expose the Singularity /tmp fallback behavior through the recipe()
API and forward it to global configuration.
Document the Singularity 3.6 and later restriction on staging files to /tmp
and /var/tmp via %files, and describe the automatic %setup fallback
behavior enabled by default.

Also document the copy primitive _exclude_from option, including its
rsync-based %setup behavior for selective file exclusion.
Copy link
Collaborator

@samcmill samcmill left a comment

Choose a reason for hiding this comment

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

Thanks, looks great!

@samcmill samcmill merged commit 2d490e7 into NVIDIA:master Dec 17, 2025
15 checks passed
@allansp84 allansp84 deleted the feature/singularity-tmp-setup-fallback branch December 17, 2025 17:18
@allansp84
Copy link
Contributor Author

Thanks a lot for the review and for merging this, @samcmill. I really appreciate the feedback and guidance throughout the process! Best!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants