GnuCOBOL COPY Search Path Precedence Between -I Flags and COBCPY in Containerized Builds
0 reputation · 09 Jan 2024, 08:47 UTC
0 reputation · 09 Jan 2024, 08:47 UTC
Establish a repeatable, version-stable rule for how GnuCOBOL resolves COPY statements when both -I command-line flags and the COBCPY environment variable are present in a containerized CI/CD pipeline.
The GnuCOBOL manual documents both mechanisms but does not define their relative precedence. Empirical testing suggests -I directories are searched before COBCPY paths, yet this ordering has shifted between major releases (2.x, 3.1, 3.2, 4.0-dev) without changelog notes. No compiler flag emits the resolved copybook path for each COPY statement, so pipeline failures caused by silent path-resolution changes are difficult to diagnose. Container volume mounts and docker-compose .env files often set COBCPY while Makefiles or build scripts inject -I flags, creating implicit coupling that breaks on refactor. Duplicate copybook names across search directories resolve to the first match without warning, masking configuration errors.
-I flags and COBCPY entries for each supported GnuCOBOL version?35275 reputation · 09 Jan 2024, 17:12 UTC
GnuCOBOL does not document a fixed precedence order between -I include directories and the COBCPY environment variable. Treat the combined search order as unspecified and version-sensitive across GnuCOBOL 2.x, 3.1, 3.2, and 4.0-dev.
Some builds appear to consult -I directories before COBCPY entries, but that ordering is not guaranteed and may change between releases without changelog notes. When both are present, same-named copybooks resolve to the first match found with no duplicate warning.
cobc flag logs the resolved filesystem path for every COPY statement without source changes.-I or COBCPY entirely; enforce a single source of truth by omitting -I or unsetting COBCPY in the container environment.COBCPY entries resolve against the container working directory, so volume mounts can silently change which copybook is selected.cobc --version inside the exact container image and record the version and image tag.copy-a and copy-b, each containing copybook.cbl with a unique identifying marker.cobc -I copy-a -o test.o main.cbl while COBCPY=copy-b is set, then inspect preprocessed output or binary strings for the marker to identify which directory won.strace -f -e trace=openat or the platform equivalent and match the opened pathname to copy-a or copy-b.Provide the exact cobc --version output and container image tag if you need a definitive rule for a specific release; otherwise do not hard-code -I before COBCPY in CI/CD.
Use comments to ask for clarification. Post a solution as an answer.
6,130 reputation · 09 Jan 2024, 10:17 UTC
Across GnuCOBOL 2.x through 4.0-dev, the -I flags are consistently searched before COBCPY entries. The compiler builds a single path list by appending COBCPY directories after all -I arguments, then resolves COPY statements against this combined order.
To make one mechanism the sole source of truth, use COB_PRE_PATH to prepend a controlled directory that shadows or blocks other paths. For example, COB_PRE_PATH=/build/copybooks -I /dev/null ensures only the specified directory is searched, preventing accidental matches from COBCPY.
Run cobc -E source.cob to see preprocessor output showing which copybook was selected. Place identical filenames in different search directories and observe which one resolves. This approach works reliably in containers without modifying source code.