Skip to content

Fail early in *nix configuration build script #16717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

hakre
Copy link
Contributor

@hakre hakre commented Nov 6, 2024

Adding two exit early safeguards in the *nix configuration build script:

  1. Given the initial cd into the build tree fails (the project root),
    the buildconf script exits with non-zero status (failure).
  2. Given the grep command does not exist or configure.ac AC_INIT 1
    expectations are unmet, the buildconf script exits non-zero.

Additionally quoting the pathname to cd into and the empty CD_PATH parameter for portability, also for systems that are using a non-portable pathname 2 for the build tree.

Rationale:

CD-ing into the project root should always prematurely exit w/ FAILURE as a required precondition for its invocation has not been met. This should never go unnoticed as it always requires user intervention.

Similar and more specifically to the PHP build on *nix systems, the grep command is required early to obtain the php_extra_version from configure.ac. Previously, if the grep command is missing (or failing due to not matching the line with the AC_INIT macro 1), the internal dev parameter would always be zero (0) which can easily result in the situation that the configure script is not being rebuilt. This is cumbersome as the rebuild of a configure script is more likely required with checked-out dev versions under change rather than an already properly set-up build environment on a dedicated build or release system. Missing the fact that either the grep utility is missing or the expectation of having the AC_INIT macro in configure.ac is unmet should never go unnoticed as it always requires user intervention.

@cmb69
Copy link
Member

cmb69 commented Nov 7, 2024

Thank you! Makes generally sense to me, but if we explicitly set CDPATH to empty here, we should do that for the other occurences too (see https://heap.space/search?project=php-src&full=&defs=&refs=CDPATH&path=&hist=&type=).

@hakre
Copy link
Contributor Author

hakre commented Nov 7, 2024

Thank you! Makes generally sense to me, but if we explicitly set CDPATH to empty here, we should do that for the other occurences too (see https://heap.space/search?project=php-src&full=&defs=&refs=CDPATH&path=&hist=&type=).

Yes, +4 more files in the update, but please mind the target branch of this merge request: ext/fileinfo/generate_patch.sh is on a different branch.

@cmb69
Copy link
Member

cmb69 commented Nov 7, 2024

ext/fileinfo/generate_patch.sh is on a different branch.

Ah, indeed; and that's probably the least important one (only used maybe once a year).

hakre added a commit to hakre/php-src that referenced this pull request Nov 7, 2024
Adding one more [1] exit early safeguard in *nix build scripts:

Given the initial cd into the build tree fails (the project root),
   the `buildconf` script exits with non-zero status (failure).

Additionally quoting the pathname to cd into and the empty CD_PATH
parameter for portability, also for systems that are using a
non-portable pathname [2] for the build tree.

[1]: php#16717
[2]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_271
hakre added a commit to hakre/php-src that referenced this pull request Nov 7, 2024
Adding one more [1] exit early safeguard in *nix build scripts:

Given the initial cd into the build tree fails (the project root),
   the `buildconf` script exits with non-zero status (failure).

Additionally quoting the pathname to cd into and the empty CD_PATH
parameter for portability, also for systems that are using a
non-portable pathname [2] for the build tree.

[1]: php#16717
[2]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_271
@hakre
Copy link
Contributor Author

hakre commented Nov 7, 2024

Ah, indeed; and that's probably the least important one (only used maybe once a year).

And thinking: Nevertheless this change should be complete in the sense you highlighted at first:

@petk
Copy link
Member

petk commented Nov 7, 2024

Thanks @hakre all looks good to me. Except in case of failure there now wouldn't be any info why some script failed.

Copy link
Member

@petk petk left a comment

Choose a reason for hiding this comment

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

Ok, never mind the previous comment. I guess the error is emitted anyway in case of failure.

@hakre
Copy link
Contributor Author

hakre commented Nov 7, 2024

Thanks @hakre all looks good to me. Except in case of failure there now wouldn't be any info why some script failed.

Ok, never mind the previous comment. I guess the error is emitted anyway in case of failure.

Yes, there should be diagnostics for cd(1) "The cd utility shall then perform actions equivalent to the chdir() function called with curpath as the path argument. If these actions fail for any reason, the cd utility shall display an appropriate error message and the remainder of this step shall not be executed." ref.

For grep there is no diagnostic message, only the non-zero exit status. This suffices to fail early so the operation is as announced.

For the principle of the least astonishment I'll add a message there.

Please also take a look at PR #16724 and if possible assign yourself as reviewer there @petk

@hakre
Copy link
Contributor Author

hakre commented Nov 7, 2024

Argh, adding diagnostics for the grep is a bit in muddy waters. If the grep command does not exists, we get diagnostics for that. So that is one of two and fine.

If the AC_INIT macro could not be found in ./configure.ac then there is no diagnostic message. Technically this can only happen when it was edited and the line was removed in error (or similar). IMHO this does not require any further diagnostics as there is already ongoing user-interaction (most likely) and failing fast is the benefit. Additionally it would require to filter the other error condition (grep command not found) to give an appropriate message which is a bit over the top, isn't it? @petk

Let me know what is wanted, I can implement it but my current assumption is that it is not really helpful at this point.

@petk
Copy link
Member

petk commented Nov 7, 2024

It's ok. If grep isn't found it is already enough info as this grep in the buildconf should work across all implementations I think.

hakre added a commit to hakre/php-src that referenced this pull request Nov 7, 2024
Adding one more [1] exit early safeguard in *nix build scripts:

Given the initial cd into the build tree fails (the project root),
   the `buildconf` script exits with non-zero status (failure).

Additionally quoting the pathname to cd into and the empty CD_PATH
parameter for portability, also for systems that are using a
non-portable pathname [2] for the build tree.

[1]: php#16717
[2]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_271
@hakre
Copy link
Contributor Author

hakre commented Nov 7, 2024

I'll push again, two Win32 tests on filenames look flaky: https://github.com/php/php-src/actions/runs/11730392143/job/32678161308#step:6:418

Adding two exit early safeguards in the *nix configuration build script:

1) Given the initial cd into the build tree fails (the project root),
   the `buildconf` script exits with non-zero status (failure).
2) Given the grep command does not exist or `configure.ac` AC_INIT [1]
   expectations are unmet, the buildconf script exits non-zero.

Additionally quoting the pathname to cd into and the empty CD_PATH
parameter for portability, also for systems that are using a
non-portable pathname [2] for the build tree.

The initial CD safeguard has been applied to the `buildconf` and
four more scripts:

- build/genif.sh
- scripts/dev/credits
- scripts/dev/genfiles
- scripts/dev/makedist

Rationale:

Cd-ing into the project root should always prematurely exit w/ FAILURE
as a required precondition for its invocation has not been met. This
should never go unnoticed as it always requires user intervention.

Similar and more specifically to the PHP build on *nix systems, the
grep command is required early to obtain the `php_extra_version` from
configure.ac.  Previously, if the grep command is missing (or failing
due to not matching the line with the AC_INIT macro [1]), the internal
dev parameter would always be zero (0) which can easily result in the
situation that the configure script is not being rebuilt. This is
cumbersome as the rebuild of a configure script is more likely required
with checked-out dev versions under change rather than an already
properly set-up build environment on a dedicated build or release
system. Missing the fact that either the grep utility is missing or
the expectation of having the AC_INIT macro in configure.ac is unmet
should never go unnoticed as it always requires user intervention.

[1]: https://www.gnu.org/software/autoconf/manual/autoconf-2.67/html_node/Initializing-configure.html
[2]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_271
@cmb69
Copy link
Member

cmb69 commented Nov 7, 2024

two Win32 tests on filenames look flaky

Needed to backport d4263dd (not sure why I had targeted master back then): 03eeec1

@cmb69 cmb69 closed this in c075546 Nov 9, 2024
@cmb69
Copy link
Member

cmb69 commented Nov 9, 2024

Thank you! Applied to PHP-8.2 and merged up.

cmb69 pushed a commit that referenced this pull request Nov 9, 2024
Adding one more [1] exit early safeguard in *nix build scripts:

Given the initial cd into the build tree fails (the project root),
   the `buildconf` script exits with non-zero status (failure).

Additionally quoting the pathname to cd into and the empty CD_PATH
parameter for portability, also for systems that are using a
non-portable pathname [2] for the build tree.

[1]: #16717
[2]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_271

Closes GH-16724.
@hakre hakre deleted the build-fail-early branch November 9, 2024 20:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants