Skip to content
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

lib.strings: Prevent paths as inputs in some functions #221204

Merged
merged 3 commits into from
Apr 4, 2023

Conversation

infinisil
Copy link
Member

Description of changes

Some functions in lib.strings could lead to unexpected results when path data types are passed. This is confusing with lib.path now existing and being extended.

# Always returns false
nix-repl> lib.strings.hasPrefix ./lib ./lib
false

# No point, since absolute paths are always normalised, normalizePath is explicitly only useful for strings
nix-repl> lib.strings.normalizePath ./lib
"/nix/store/6d499zmgypxx73c7gdm3b36mkxy21iis-lib"

This work is sponsored by Antithesis

@infinisil infinisil requested a review from edolstra as a code owner March 14, 2023 19:18
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10 labels Mar 14, 2023
lib/strings.nix Outdated Show resolved Hide resolved
lib/strings.nix Outdated Show resolved Hide resolved
lib/strings.nix Outdated Show resolved Hide resolved
lib/strings.nix Outdated
Comment on lines 249 to 250
if isPath pref then throw ''
lib.strings.hasPrefix: The first argument (${toString pref}) is a path, which is not supported''
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if isPath pref then throw ''
lib.strings.hasPrefix: The first argument (${toString pref}) is a path, which is not supported''
# Before 23.05, strings would be added to the store before comparing. This was too unexpected.
if isPath pref then throw ''
lib.strings.hasPrefix: The first argument (${toString pref}) is a path value, but only strings are supported.''

Leave a clue in case users encounter this in their code base, so they know something has changed here. It appears in the code block of the stack trace iirc. The exception message does not include the whole history, because I don't expect that to be productive. It could stick around for years being unnecessarily noisy.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds good, but "only strings are supported" isn't entirely correct, all stringLike values (including outPath, __toString) are supported, except paths. Maybe that's good enough though

Copy link
Member

Choose a reason for hiding this comment

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

Right. Probably best to keep it simple and avoid unnecessary dissonance?
This can only be cured with poetry. Cue ChatGPT:

Here is a revised limerick about type coercions in programming:

Type coercion can be quite slick,
To convert data types that don't mix.
But if you're not careful,
Your code can be awful,
And bugs will give your code a big kick.

Copy link
Member Author

Choose a reason for hiding this comment

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

Haha nicely done 😆

I agree, let's keep it simple, I applied this suggestion to all the error messages (also the normalizePath one).

lib/strings.nix Outdated Show resolved Hide resolved
lib/strings.nix Outdated Show resolved Hide resolved
@infinisil infinisil force-pushed the deprecate-paths-to-strings branch 2 times, most recently from f2d6bd4 to d9cbdc5 Compare March 14, 2023 22:29
Copy link
Contributor

@fricklerhandwerk fricklerhandwerk left a comment

Choose a reason for hiding this comment

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

This is a breaking change and at the very least needs release notes. Almost certainly this will error out on someone in production. We may even want to phase this in with a warning or setting. Who else should we ask to make that decision? @roberth opinions?

lib/strings.nix Outdated Show resolved Hide resolved
@infinisil infinisil force-pushed the deprecate-paths-to-strings branch from d9cbdc5 to 7101e4b Compare March 14, 2023 23:22
@infinisil
Copy link
Member Author

Hmm yeah I guess it's a breaking change, even if the code we break is certainly a bug (in case of hasPrefix), or unnecessary (in case of normalizePath).

I changed the errors to a warning instead and added some more explanatory text to them.

@infinisil infinisil force-pushed the deprecate-paths-to-strings branch 3 times, most recently from addf8c4 to c518535 Compare March 14, 2023 23:42
lib/strings.nix Outdated Show resolved Hide resolved
@fricklerhandwerk
Copy link
Contributor

Have to take the space bar heaters into account... been there myself.

lib.{hasPrefix,hasInfix,hasSuffix} would otherwise return an
always-false result, which can be very unexpected:

    nix-repl> lib.strings.hasPrefix ./lib ./lib/meta.nix
    false
There's no need to call this function on path data types, and it's
confusing with the new lib.path library functions
@infinisil infinisil force-pushed the deprecate-paths-to-strings branch from c518535 to 5e8b9de Compare March 15, 2023 18:42
@infinisil
Copy link
Member Author

I also noticed that removePrefix and removeSuffix suffered from the same problems, so I added the warning to those as well.

@infinisil infinisil force-pushed the deprecate-paths-to-strings branch from 15a8230 to 61012f6 Compare March 15, 2023 18:51
@infinisil
Copy link
Member Author

I think this looks good and would like to merge it soon

@infinisil infinisil requested a review from roberth March 21, 2023 17:35
@infinisil infinisil merged commit 4f35a58 into NixOS:master Apr 4, 2023
@infinisil infinisil deleted the deprecate-paths-to-strings branch April 4, 2023 16:35
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/tweag-nix-dev-update-47/27387/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin 10.rebuild-linux: 1-10
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants