Skip to content

Fix header include path for output outside the current directory - #877

Merged
ydah merged 1 commit into
ruby:masterfrom
wtn:include
Nov 21, 2026
Merged

ydah merged 1 commit into
ruby:masterfrom
wtn:include

Conversation

@wtn

@wtn wtn commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Lrama::Output builds the #include path with header_file_path.sub("./", ""). String#sub with a string pattern replaces the first occurrence of the pattern, so ../y.tab.h becomes .y.tab.h.

$ lrama -d -o ../y.tab.c parse.y
$ grep '#include "' ../y.tab.c
#include ".y.tab.h"

When the header and the output file are in the same directory on disk, the include is now the header's base name, which is what Bison 3.8.2 emits. The directories are compared with File.identical?, which follows symlinks before applying ... For -o build/link/../parser.c with build/link a symlink, the comparison uses the symlink target's parent, where the compiler looks for a quoted include. The change also turns -d -o ext/y.tab.c from #include "ext/y.tab.h" into #include "y.tab.h". When the two files are in different directories, the path is kept as given and only a leading ./ is removed.

@ydah

ydah commented Nov 21, 2026

Copy link
Copy Markdown
Member

Could we check whether the two directories refer to the same directory on disk before using the header’s basename? File.absolute_path collapses .. without resolving symlinks, so two different directories can compare equal here.

I reproduced this with:

mkdir -p build actual/sub
ln -s ../actual/sub build/link
lrama -o build/link/../parser.c --header="$PWD/build/parser.h" grammar.y
cc -c build/link/../parser.c

The C file ends up in actual/, while the header is in build/. Before this change, Lrama preserved the absolute header path and compilation succeeded. With this change, it emits #include "parser.h", and the compiler can’t find the header.

Could we add a regression test for this case as well?

Co-authored-by: Claude <noreply@anthropic.com>
@wtn

wtn commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for the review and the reproduction steps. I could reproduce the problem.

I changed the comparison to use File.identical?. File.identical? checks both directories with stat, so the build/link symlink is resolved before .. is applied. In your example, the two directories are different, so Lrama keeps the absolute header path like before. I also added a regression test with the same symlink layout.

Bison 3.8.2 outputs #include "parser.h" in the same case. Bison always uses the base name of the header, even when the header is in a different directory.

@ydah
ydah merged commit 8864d2b into ruby:master Nov 21, 2026
23 checks passed
@wtn
wtn deleted the include branch November 21, 2026 01:26
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