-
Our team would like to add a Yarn Constraints rule to enforce script names only containing colons if they are globally unique (i.e. only use a colon if you meant for this to be a global script). The constraint that I thought would work is this: gen_enforced_field(WorkspaceCwd, Field, null) :-
workspace_field(WorkspaceCwd, Field, _),
atom_concat("scripts.", ScriptName, Field),
sub_atom(ScriptName, _, _, _, ":"),
workspace_field(OtherWorkspaceCwd, Field, _),
WorkspaceCwd \= OtherWorkspaceCwd. However, this results in an error:
After trying a lot of other things, I found that even this simple query fails. $ # query for all workspace scripts
$ yarn constraints query "workspace(W), workspace_field(W, F, _), atom_concat('scripts.', _, F)."
➤ YN0001: TypeError: r.throwError is not a function
at workspace_field/3 (/<repository>/.yarn/plugins/@yarnpkg/plugin-constraints.cjs:28:29229)
at X.step (/<repository>/.yarn/plugins/@yarnpkg/plugin-constraints.cjs:26:11332)
at X.again (/<repository>/.yarn/plugins/@yarnpkg/plugin-constraints.cjs:26:12620)
at X.answer (/<repository>/.yarn/plugins/@yarnpkg/plugin-constraints.cjs:26:12019)
at D.answer (/<repository>/.yarn/plugins/@yarnpkg/plugin-constraints.cjs:26:11906)
at /<repository>/.yarn/plugins/@yarnpkg/plugin-constraints.cjs:28:31869
at new Promise (<anonymous>)
at ti.fetchNextAnswer (/<repository>/.yarn/plugins/@yarnpkg/plugin-constraints.cjs:28:31840)
at ti.makeQuery (/<repository>/.yarn/plugins/@yarnpkg/plugin-constraints.cjs:28:31983)
at makeQuery.next (<anonymous>) What am I doing wrong? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
FWIW, I use https://swish.swi-prolog.org/ as a poor-man's REPL for trying out rules. I set up a rules DB with the following contents: workspace("w1").
workspace("w2").
workspace_field("w1", "scripts.foo", "script contents").
workspace_field("w1", "scripts.g:bar", "script contents").
workspace_field("w2", "scripts.g:bar", "script contents").
gen_enforced_field(WorkspaceCwd, Field, null) :-
workspace_field(WorkspaceCwd, Field, _),
atom_concat("scripts.", ScriptName, Field),
sub_atom(ScriptName, _, _, _, ":"),
workspace_field(OtherWorkspaceCwd, Field, _),
WorkspaceCwd \= OtherWorkspaceCwd. And then I executed the following query (which I copied from this in the constraints source code: ?- workspace(WorkspaceCwd), gen_enforced_field(WorkspaceCwd, FieldPath, FieldValue). This results in the expected requirements set:
|
Beta Was this translation helpful? Give feedback.
-
Ah, I think I see now. The My Similarly, the query I ran failed because I didn't have enough arguments. And finally, my poor approximation of testing these rules was incorrect because constraints does not actually create facts for the existing workspace fields, which would have made the fields already provided. |
Beta Was this translation helpful? Give feedback.
Ah, I think I see now. The
FieldPath
in theworkspace_field
query predicate needs to be instantiated.My
gen_enforced_field
rule is invalid becauseField
is not bound to fields in each workspace and narrowed by theatom_concat
andsub_atom
predicates like I assumed, but instead this is attempting to instantiate it to "all strings starting with 'scripts.' and including a colon" which is an infinite list (triggering the error).Similarly, the query I ran failed because I didn't have enough arguments.
And finally, my poor approximation of testing these rules was incorrect because constraints does not actually create facts for the existing workspace fields, which would have made the fields al…