Subject: Re: [PATCH v5 07/45] guix: node-build-system: Add #:absent-dependencies argument.

From: Philip McGrath <[email-masked]>
Date: Sat, 18 Dec 2021 13:31:40 -0500

Hi,

On 12/18/21 03:30, Liliana Marie Prikler wrote:
> Hi,
> 
> Am Freitag, dem 17.12.2021 um 21:48 -0500 schrieb Timothy Sample:
>> Liliana Marie Prikler <[email-masked]> writes:
>>
>>> For the GNU build system (and likewise meson-build-system), the
>>> default behaviour if you haven't specified anything as per upstream
>>> conventions is typically to error if the package is required and
>>> omit it if it's not.  The default behaviour of node-build-system
>>> (and likewise cargo and most other build systems that come with the
>>> advertisement of "we know package managers better than people who
>>> actually produce usefulpackage managers) is "Oh my god, you don't
>>> have an exact copy of the machine that built this stuff locally, I
>>> am going to barf huge walls of noise at you".  Therefore, we can't
>>> meaningfully compare those build systems in terms of strategies.
>>
>> NPM packages tend to wildly over-specify their dependencies.  We
>> already remove dependency version checking, and before this change,
>> many of our packages skipped any kind of dependency checking by
>> skipping the configure phase altogether.  To me, the ‘#:absent-
>> dependencies’ approach tries to work around the dependency over-
>> specification by listing exactly those things that are only there to
>> elicit a useless “Oh my god [...], I’m going to barf huge walls of
>> noise” message.  The rest of the dependencies are those that the Guix
>> package author deemed required (or at least important).  Basically,
>> ‘#:absent-dependencies’ helps us translate between the NPM culture of
>> over-specification (which is really a culture of prioritizing package
>> author over package user) and the GNU culture of “DWIM” dependencies.
> Except that it's not.  The current workaround is "I know this thing's
> going to barf at me, so I prepare an umbrella and hope it has no
> holes".
> 
>>> If we really want some static verification for node-build-system, I
>>> think we should take that as an approach rather than hard-coding
>>> (absent) dependencies literally everywhere.
>>
>> We need some way to know what to statically verify.  We can’t
>> magically know what’s important and what isn’t.  The two options in
>> this thread are ‘#:absent-dependencies’, and only checking what’s
>> already in the package’s inputs.  What worries me about the second
>> approach is that it offers no help when updating a package.  With
>> ‘#:absent-dependencies’, if the developer adds a new dependency that
>> really is required, we will get a build-time failure letting us
>> know.  Whoever is updating the package can fix it before even
>> committing anything.  If we just check the inputs, that’s not the
>> case, and we might end up with Philip’s “mysterious runtime error,
>> potentially many steps down a dependency chain.”  Hopefully tests
>> would catch it, but I like the extra assurance.
> That's why I didn't want to default to "do nothing", but to *warn*
> about missing dependencies in configure.  Then whoever bumps the
> package will at least know which warnings are produced if they do so
> and they can cross-check by manually building past versions.  Including
> #:absent-dependencies is no safe-guard against a failure here anyway.
> A dependency that was optional in V1 might become required in V2.

I also feel like I'm missing something, though maybe I just disagree.

To try to be concrete, here's a real example. Between v3 and v4 of this 
patch series, I discovered that leaving out node-debug could actually 
cause runtime problems for some of the node-serialport packages. (It 
turns out it's really a logging library, which wasn't what I'd thought 
at first.) After I added the node-debug, I could easily search 
node-xyz.scm for the packages that listed it among their 
#:absent-dependencies and add it to them as an input.

It seems like this would be much less convenient if node-build-system 
were to silently delete such dependencies and simply print a warning. I 
guess I would have to search through the build logs for all Node.js 
packages, right?

More generally, I think truly "optional dependencies" (in the Node.js 
sense, to the extent I understand it) and dependencies we actively don't 
want (e.g. because node-sqlite3 shouldn't transitively depend on 
node-aws-sdk) are relatively rare cases.

The most common cases seem to be dependencies we really would like to 
have, such as test frameworks or Typescript, but haven't packaged yet, 
and thus need to work around. Many packages that have 
#:absent-dependencies for such reasons also need to use `#:tests? #f` or 
to replace the build phase with some kind of manual alternative.

I guess I don't understand what case the warn-and-drop approach is 
optimizing for. For both the case when dependencies aren't packaged for 
Guix yet and the case when Guix packagers have actively decided to skip 
some upstream dependencies, I think #:absent-dependencies is more 
useful. Having to look for that information in warnings in the build log 
seems much less ergonomic.

>> Another benefit is that it would help us gain knowledge as to which
>> NPM packages are often used but not actually required (e.g., NPM
>> publishing tools).  With this knowledge, we could write a clever NPM
>> importer that ignored obviously inessential dependencies.

I agree with this, too: likewise, we could see packages that are often 
wanted but aren't in Guix and prioritize adding them! Part of the 
benefit of #:absent-dependencies, IMO, is as a form of communication 
with humans.

>> I guess I’m starting to sound like a broken record now – this is
>> basically what we covered before!  :)  Maybe we’re in need of a fresh
>> perspective.  (If anyone is reading along and has thoughts, feel free
>> to chime in!)
> I think the NPM convention is to put everything you need "at build
> time, but not at runtime" into dev-dependencies, no?  In any case, one
> approach I could offer is to sanity-check by searching for require()
> statements and trying them in a controlled node environment.  This
> could look something like
> 
> eval("try { var dep = require('" + dependency + "'); true }
> catch (e) { false; }")
> 
> Once we know where require statements are made and whether they
> succeed, we can start estimating the impact of a missing dependency.
> For this, it'd be nice to have a full function call graph, in which a
> node is coloured dirty if it has a failing require statement, lies
> within a module that has one or calls into a dirty node.  However, as a
> primitive approximation we can also count the node modules with failing
> requires against those that don't.  We set an arbitrary threshold of
> allowed failures, e.g. 0.42, and then check that whatever value we
> obtain from the above is lower than that.

This could be interesting, and I think some of the JavaScript blunders 
we don't have packaged for Guix yet try to do something like this, but 
such analysis is not tractable in the general case, especially with 
CommonJS `require`, which is just a function and can be given arbitrary 
arguments computed at runtime. (And some packages really use it that way!)

Also, currently node-build-system doesn't seem to be removing those 
files which `npm pack` is supposed to exclude, which would probably be a 
prerequisite for addressing this.

> While that would be nice and all, I think the overall issue with the
> current node implementation in Guix is that 'configure' and 'sanity-
> check' are the same phase, so you have to disable both or none.  I
> think we could easily do with a configure phase that does nothing, not
> even warn about a missing dependency, and a sanity-check phase that
> requires every dependency mentioned in package.json to be met.
> Packagers would then outright delete sanity-check as they do for python
> and as they did for configure (but not have configure fail due to it!)
> or deliberately rewrite the package.json for the sanity check and
> dropping absent dependencies, i.e. what you do minus the keyword.  If
> later needed for the purposes of an importer, we would then still have
> that database and could at some point introduce the key #:insane-
> requirements.  WDYT?

I don't understand the benefit of this, and I'm also confused about the 
proposed implementation specifics. Why even have "a configure phase that 
does nothing"? What phase would run `npm install`? Presumably, we would 
have to delete all missing dependencies before that.

I think there is room for improvement in node-build-system. One thing 
I've been thinking about is some articles I've seen (but not fully 
thought through yet) from the developers of PNPM, an alternative package 
manager for Node.js, which seems to have some similarities to Guix in 
symlinking things to a "store".[1][2][3] (It could be especially 
interesting for bootstrapping npm! And the approach to "monorepos" also 
seems relevant.) I also think an importer is very important, even if 
it's an imperfect one: `guix import npm-binary` was indispensable in 
developing these patches. I have some ideas about improving it, in 
particular that we should assume the newer "^" semantics for 
dependencies everywhere (i.e. that major versions and only major 
versions have incompatible changes: a common case recently seems to be 
moving from CommonJS modules to ES6 modules).

As I understand it, node-build-system is undocumented, with no 
guarantees of compatibility. If #:absent-dependencies is at least an 
improvement over the status quo---which I think it is, since the new 
packages wouldn't build with the status quo---could we apply this and 
replace it later if someone implements a better strategy? I don't think 
I can implement control-flow analysis for JavaScript within the scope of 
this patch series.

-Philip

[1]: https://pnpm.io/blog/2020/05/27/flat-node-modules-is-not-the-only-way
[2]: https://pnpm.io/symlinked-node-modules-structure
[3]: https://pnpm.io/how-peers-are-resolved