Skip to content

awk: report an unreadable -f file and run --source programs - #188

Open
VanshajPoonia wants to merge 4 commits into
uutils:mainfrom
VanshajPoonia:awk-f-and-source-171
Open

VanshajPoonia wants to merge 4 commits into
uutils:mainfrom
VanshajPoonia:awk-f-and-source-171

Conversation

@VanshajPoonia

Copy link
Copy Markdown
Contributor

Both ways of giving the program as an option crash as the issue shows:

$ awk -f missing.awk
thread 'main' panicked at parser/src/lib.rs:1037:38:
called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, ... }
$ awk --source 'BEGIN { print 1 }'
thread 'main' panicked at src/main.rs:51:43:
called `Option::unwrap()` on a `None` value
  • A -f file that can't be read now prints awk: fatal: cannot open source file `missing.awk' for reading: No such file or directory and exits with 2, like gawk in the issue. The parser's file reader returns the error instead of calling unwrap().
  • With -f or --source there is no program operand, so the first operand is an input file, as the gawk manual describes. It was taken as the program instead: awk -f prog.awk data ignored data and read stdin, and --source alone had nothing to parse.
  • The --source texts are now the program. Each one is read as if it ended with a newline, which the manual also says.

Left out: -f and --source together still use only the -f files. An unreadable @include file still panics (marked TODO), since I couldn't check what gawk does there. --debug with either option still panics (#168).

exit_fatal and strip_os_error in src/utils.rs are the same as in #187, so the two merge cleanly in either order.

Tests: five new tests (missing -f file, -f and --source each with an input operand, --source alone, two -e texts), all fail on main. cargo test --workspace 230 passed, cargo clippy --workspace --all-targets -- -D warnings and cargo fmt --all -- --check clean.

Closes #171

@github-actions

Copy link
Copy Markdown

GNU awk testsuite comparison:

Test results comparison:
  Current:   TOTAL: 666 / PASSED: 106 / FAILED: 462 / SKIPPED: 98
  Reference: TOTAL: 666 / PASSED: 104 / FAILED: 464 / SKIPPED: 98

Changes from main branch:
  TOTAL: +0
  PASSED: +2
  FAILED: -2

Test improvements (2):
  + negtime
  + profile12

@sylvestre

Copy link
Copy Markdown
Contributor

thanks for picking this up!
Not ready yet: when -f and --source are both given, the --source texts are ignored silently, and @include can still panic (details inline).
Please also squash the 3 commits into one.

Comment thread parser/src/lib.rs Outdated
pub fn include_many(
&mut self,
files: &[PathBuf],
) -> Result<Result<&mut Ast<'a>, DiagnosticStore>, (Rc<Path>, io::Error)> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

a Result<Result<..>> is hard to read
could you please use a thiserror enum (or put the io error into the diagnostics) instead?

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.

Replaced with an IncludeError thiserror enum: CannotOpen { path, err } for a -f file that can't be read, Invalid(DiagnosticStore) for parse errors.

Comment thread parser/src/lib.rs Outdated

fn include(&mut self, path: Rc<Path>, ns: Option<&'static str>) -> (Result<()>, FileCache) {
// TODO: report an unreadable @include file instead of panicking.
let source = self.preprocessor.read_to_arena(&path, self.arena).unwrap();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why not return the error here too? same message as for -f, no?
we shouldn't leave an unwrap here

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.

No unwrap left. An @include that can't be read is now a ParsingError::CannotOpenSource diagnostic at the include path, with the same message as -f. Like every parse diagnostic on main today, it still exits 0.

Comment thread src/main.rs Outdated
Some(code) => code.as_encoded_bytes(),
None => &source_text,
};
parser.parse(FileCache(None), code)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

with both -f and --source, the --source texts are silently ignored
could you please concatenate them like gawk, or at least error out?

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.

They're read in command-line order now, like gawk: the -f files and --source texts are parsed as one program in the order given, and adjacent --source texts stay together so a construct can span them. A test covers both orders.

@VanshajPoonia

Copy link
Copy Markdown
Contributor Author

Thanks! Squashed into one commit and rebased on main.

@github-actions

Copy link
Copy Markdown

GNU awk testsuite comparison:

Test results comparison:
  Current:   TOTAL: 666 / PASSED: 106 / FAILED: 462 / SKIPPED: 98
  Reference: TOTAL: 666 / PASSED: 104 / FAILED: 464 / SKIPPED: 98

Changes from main branch:
  TOTAL: +0
  PASSED: +2
  FAILED: -2

Test improvements (2):
  + negtime
  + profile12

@github-actions

Copy link
Copy Markdown

GNU awk testsuite comparison:

Test results comparison:
  Current:   TOTAL: 666 / PASSED: 108 / FAILED: 460 / SKIPPED: 98
  Reference: TOTAL: 666 / PASSED: 106 / FAILED: 462 / SKIPPED: 98

Changes from main branch:
  TOTAL: +0
  PASSED: +2
  FAILED: -2

Test improvements (2):
  + negtime
  + profile12

Comment thread src/main.rs Resolved
Comment thread src/main.rs Outdated

/// The -f files and --source texts in command-line order, as gawk reads them.
/// Adjacent --source texts stay one piece, so a construct can span them.
fn program_sources<'a>(matches: &clap::ArgMatches, args: &'a Args) -> Vec<ProgramSource<'a>> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think it would be better to attempt to do this as clap item parser, so we don't have to post-process clap's output.

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.

Done in ProgramSources, a clap Args impl that gives the -f files and --source texts in order. clap keeps the two options in separate lists so it still reads the indices. But main.rs is back to Args::try_parse_from.

Comment thread parser/src/lib.rs Resolved
@github-actions

Copy link
Copy Markdown

GNU awk testsuite comparison:

Test results comparison:
  Current:   TOTAL: 666 / PASSED: 108 / FAILED: 460 / SKIPPED: 98
  Reference: TOTAL: 666 / PASSED: 106 / FAILED: 462 / SKIPPED: 98

Changes from main branch:
  TOTAL: +0
  PASSED: +2
  FAILED: -2

Test improvements (2):
  + negtime
  + profile12

Comment thread tests/by-util/test_awk.rs Outdated
.write("prog.awk", "@include \"no-such-include.awk\"\n");
ts.cmd(crate::TESTS_BINARY)
.args(&["-f", "prog.awk"])
.run()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

could you please check the exit code here too?
gawk exits 2 on an @include it can't open (fatal), we exit 1 as a parse error

@github-actions

Copy link
Copy Markdown

GNU awk testsuite comparison:

Test results comparison:
  Current:   TOTAL: 666 / PASSED: 108 / FAILED: 460 / SKIPPED: 98
  Reference: TOTAL: 666 / PASSED: 106 / FAILED: 462 / SKIPPED: 98

Changes from main branch:
  TOTAL: +0
  PASSED: +2
  FAILED: -2

Test improvements (2):
  + negtime
  + profile12

@VanshajPoonia

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I added the exit code check to the test.
On gawk 5.4.2 an @include it can't open, prints an error and exits 1. Only a missing -f file or -i is fatal with exit 2. We match that and the test now expects 1. Which gawk did you run? If yours exits 2 here I will make it fatal.

Comment thread src/utils.rs Outdated
}

/// Prints a fatal error and exits with 2, gawk's status for fatal errors.
pub fn exit_fatal(msg: impl Display) -> ! {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

exit_fatal was removed from main in c0ac1db
could you please use exit_err(Some(format!("awk: fatal: {err}")), AWK_FATAL_CODE) in main.rs instead?

Comment thread parser/src/lib.rs Outdated
/// Why [`Parser::include_many`] has no program to run.
#[derive(Debug, thiserror::Error)]
pub enum IncludeError {
#[error("cannot open source file `{}' for reading: {}", .path.display(), read_reason(.err))]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

same message as ParsingError::CannotOpenSource, could be dedup, no?

-f with a file that can't be read panicked, and --source was ignored.
Read the -f files and --source texts in command-line order like gawk,
keeping adjacent --source texts together so a construct can span them.
A -f file that can't be read is fatal with gawk's message and status 2;
an @include that can't be read is a diagnostic at its path. With -f or
--source there is no program operand, so the first operand is an input.
Each -f file and --source text is now its own piece in include_many, so
an error's line number counts from the start of its own text and each
text starts in the awk namespace, like gawk. FileCache gets a Cli(n)
variant so every --source text has its own diagnostics entry.

The order of -f and --source now comes from a clap Args impl, so main.rs
no longer reads the arg matches. --source texts are borrowed from the
arguments instead of copied into the arena.
@github-actions

Copy link
Copy Markdown

GNU awk testsuite comparison:

Test results comparison:
  Current:   TOTAL: 666 / PASSED: 108 / FAILED: 460 / SKIPPED: 98
  Reference: TOTAL: 666 / PASSED: 106 / FAILED: 462 / SKIPPED: 98

Changes from main branch:
  TOTAL: +0
  PASSED: +2
  FAILED: -2

Test improvements (2):
  + negtime
  + profile12

gawk 5.4.2 reports it as an error and exits 1, not a fatal 2 like -f.
@github-actions

Copy link
Copy Markdown

GNU awk testsuite comparison:

Test results comparison:
  Current:   TOTAL: 666 / PASSED: 108 / FAILED: 460 / SKIPPED: 98
  Reference: TOTAL: 666 / PASSED: 106 / FAILED: 462 / SKIPPED: 98

Changes from main branch:
  TOTAL: +0
  PASSED: +2
  FAILED: -2

Test improvements (2):
  + negtime
  + profile12

@VanshajPoonia

Copy link
Copy Markdown
Contributor Author

Thank you, I worked on both of the comments. main.rs uses exit_err now and the "cannot open source file" message is shared. I also rebased the branch on main now.

On the exit code. I ran gawk 5.4.2 again. A missing @include prints an error and exits 1. Only a missing -f or -i file is fatal with exit 2. We match both and the test checks it. If your gawk exits 2 for @include let me know which version.

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.

awk: -f missing-file and --source panic

3 participants