:warning: The Truffle Suite is being sunset. For information on ongoing support, migration options and FAQs, visit the Consensys blog. Thank you for all the support over the years.
require() (as in, artifacts.require()... artifacts just an instance of a resolver)
resolve()
Now, the resolver's purpose is to load build artifacts, not sources, and so, when you do artifacts.require("MetaCoin"), it reads it from build/contracts/MetaCoin.json, not the Solidity. This is true even if you do artifacts.require("MetaCoin.sol"): Truffle finds the JSON file whose source was MetaCoin.sol, and that's what it loads.
And so you'd think, that the resolve() function is just a lightweight require() that returns the artifact path instead of the fully loaded contract abstraction from that path.
But that's not what resolve() does! Look: the one place we use this function inside Truffle (the profiler). The profiler uses it to figure out Solidity import statements. And it looks like it expects resolve to give the path and the loaded contents.
So, why can't we contain this functionality entirely within the profiler itself? Well, we need to provide a means to inform the profiler of additional sources at runtime, e.g. with Solidity tests' import "truffle/DeployedAddresses.sol". That source is generated from a template and never lives on disk. Truffle hooks this up to the profiler by means of the TestSource, injected into the TestResolver, so that the profiler can include that in compilation.
There is a package associated with sources, @truffle/contract-sources, but currently it only lists available sources, and incompletely so. My first thought is that this is a better home for this functionality, although there are various limitations in the way:
That package is just a static function; it doesn't get attached to the config, and so it doesn't have the same extant utility as the resolver (which is available throughout Truffle, already instantiated)
How do we reconcile the duplication of "sources"? Does that get separated into its own package, out of resolver? What does that look like?
Blech. This is sort of related to #2469, and it'd probably be worth solving both this issue and that one at the same time.
Issue
@truffle/resolver provides two main methods for external use:
require()
(as in,artifacts.require()
...artifacts
just an instance of a resolver)resolve()
Now, the resolver's purpose is to load build artifacts, not sources, and so, when you do
artifacts.require("MetaCoin")
, it reads it frombuild/contracts/MetaCoin.json
, not the Solidity. This is true even if you doartifacts.require("MetaCoin.sol")
: Truffle finds the JSON file whose source wasMetaCoin.sol
, and that's what it loads.And so you'd think, that the
resolve()
function is just a lightweightrequire()
that returns the artifact path instead of the fully loaded contract abstraction from that path.But that's not what
resolve()
does! Look: the one place we use this function inside Truffle (the profiler). The profiler uses it to figure out Solidityimport
statements. And it looks like it expectsresolve
to give the path and the loaded contents.So, why can't we contain this functionality entirely within the profiler itself? Well, we need to provide a means to inform the profiler of additional sources at runtime, e.g. with Solidity tests'
import "truffle/DeployedAddresses.sol"
. That source is generated from a template and never lives on disk. Truffle hooks this up to the profiler by means of the TestSource, injected into the TestResolver, so that the profiler can include that in compilation.There is a package associated with sources, @truffle/contract-sources, but currently it only lists available sources, and incompletely so. My first thought is that this is a better home for this functionality, although there are various limitations in the way:
Blech. This is sort of related to #2469, and it'd probably be worth solving both this issue and that one at the same time.