Exemple
args 2.7.0: Prove how package:args caches and shares mutable default list instances across parses for unprovided multi-options
Échantillon vérifié pour pub args 2.7.0: Prove how package:args caches and shares mutable default list instances across parses for unprovided multi-options.…
sha256:09efd7ba07568bb8934ab71bd5cda6503408067f4a024fd6365b7cf744124ce2
Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré.
Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes.
MIT-0
Preuves d'exécution
L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.
- Base de preuve
- Contrat signé réussi
- Reçus de vérification
- 2
- Clés de signature qui l’ont compilé
- 2
Environnement déclaré
dart linux x64 dart dart pub
Environnements des exécutions de vérification
| Environnement | Contrat | Étapes | Exécution |
|---|---|---|---|
| dart 3 · linux debian/x64 · docker ed25519:d91480838ac982c9 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · pub@1 |
2026-08-17 |
| dart 3 · linux debian/x64 · docker ed25519:2175b912ea1c23b1 | PASS | compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS CONTAINER_RUN · pub@1 |
2026-08-18 |
Cas
HOW- Objectif
- Prove how package:args caches and shares mutable default list instances across parses for unprovided multi-options
- Paquets
- Symboles
-
- ArgParser
- ArgParser.addMultiOption
- ArgResults
- ArgResults.multiOption
- ArgResults.arguments
- ArgResults.rest
- Environnement
- dart
- Créé
- 2026-08-17T06:45:46Z
Contrat
- ArgResults.multiOption returns the exact mutable defaultsTo list instance stored in Option without copying, causing identical() to return true across distinct parse calls and leaking in-place mutations into subsequent parses.
- assert mutating the list returned from an unparsed multiOption modifies the parser default for subsequent parse calls
- assert implicit default multi-options share the same empty mutable list across parse invocations
- assert explicitly provided CLI multi-options allocate a distinct unshared list that does not affect the default
- assert ArgResults.arguments and ArgResults.rest are unmodifiable collections that throw UnsupportedError on mutation
Fichiers
- NOTES.md
- csx.json
- pubspec.lock
- pubspec.yaml
- test/contract.dart
Code source
# args 2.7.0 Multi-Option Default Sharing and Mutability
## Prior Knowledge Search
A search for known solutions on package:args returned prior verified samples:
- `sha256:9c2b79c1311ca900d537157032629d24ae6621db433365d53aaa0caf30317f7b`: Proving how package:args handles options after the first positional argument with and without `allowTrailingOptions`.
- `sha256:524c2bfcd735bb3578e69cc3473ac78c127a42d307b69b9af273346500877f1a`: Preserving comma-delimited option values as single arguments by disabling `splitCommas`.
This sample investigates a distinct behavior in package:args: the lifetime, caching, and mutability of default list instances in `addMultiOption`.
## Pinned Version
- `args`: 2.7.0 (pub)
## Observed Behaviour
When an `ArgParser` defines a multi-option (`addMultiOption`), the default list (whether explicitly configured via `defaultsTo` or implicitly defaulting to `<String>[]`) is retained by reference inside the option definition.
When `ArgParser.parse` processes arguments where the multi-option is omitted:
1. `ArgResults.multiOption` returns the live, mutable list reference stored in the option definition rather than an isolated copy or an `UnmodifiableListView`.
2. Multiple invocations of `parse` on the same `ArgParser` instance return the exact same list reference (`identical` evaluates to `true`).
3. If consuming code modifies the returned list in-place (such as `results.multiOption('key').add(...)`), the internal default list in `ArgParser` is directly mutated, contaminating subsequent parses that rely on that parser's default.
4. In contrast, when the option is explicitly passed on the command line (for example `--include custom.dart`), `parse` allocates a new `List<String>` for that parse execution.
5. In addition, other collections on `ArgResults` like `arguments` and `rest` are wrapped in `UnmodifiableListView` and reject modifications by throwing `UnsupportedError`.
## Failure Mode
Fails silently with a green build: When helper functions or commands accept parsed options and append fallback items to the returned multi-option list, the parser instance retains those mutated entries silently across executions. Subsequent parses without CLI arguments receive the contaminated default list rather than the initial configured defaults.
{"case":{"believed":"ArgResults returns an isolated or unmodifiable list for multi-option default values on each parse rather than sharing a mutable reference to the parser's internal default list.","caseId":"case:sha256:a06d56b6d6585b7bc83e09388f6c27fcdb5d9a70dcb60ba1dc28b7adfcd5e67e","contract":["ArgResults.multiOption returns the exact mutable defaultsTo list instance stored in Option without copying, causing identical() to return true across distinct parse calls and leaking in-place mutations into subsequent parses.","assert mutating the list returned from an unparsed multiOption modifies the parser default for subsequent parse calls","assert implicit default multi-options share the same empty mutable list across parse invocations","assert explicitly provided CLI multi-options allocate a distinct unshared list that does not affect the default","assert ArgResults.arguments and ArgResults.rest are unmodifiable collections that throw UnsupportedError on mutation"],"goal":"Prove how package:args caches and shares mutable default list instances across parses for unprovided multi-options","kind":"HOW","packages":["pkg:pub/args@2.7.0"],"schemaVersion":1,"symbols":["ArgParser","ArgParser.addMultiOption","ArgResults","ArgResults.multiOption","ArgResults.arguments","ArgResults.rest"]},"contractCommand":["dart","test/contract.dart"],"environment":{"arch":"x64","ecosystem":"pub","executionContext":"dart","language":"dart","os":"linux","packageManager":"pub","runtime":"dart","schemaVersion":1},"license":"MIT-0","packages":["pkg:pub/args@2.7.0"],"schemaVersion":1,"symbols":["ArgParser","ArgParser.addMultiOption","ArgResults","ArgResults.multiOption","ArgResults.arguments","ArgResults.rest"],"verifierAdapter":"pub@1"}
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
args:
dependency: "direct main"
description:
name: args
sha256: d0481093c50b1da8910eb0bb301626d4d8eb7284aa739614d2b394ee09e3ea04
url: "https://pub.dev"
source: hosted
version: "2.7.0"
sdks:
dart: ">=3.5.0 <4.0.0"
name: pub_args_caching
publish_to: none
environment:
sdk: ">=3.5.0 <4.0.0"
dependencies:
args: 2.7.0
import 'package:args/args.dart';
void main() {
final parser = ArgParser()
..addMultiOption('include', defaultsTo: ['base.dart'])
..addMultiOption('flags');
// 1. Unparsed multi-options return the same mutable internal list reference across parses.
final parse1 = parser.parse([]);
final parse2 = parser.parse([]);
final list1 = parse1.multiOption('include');
final list2 = parse2.multiOption('include');
if (!identical(list1, list2)) {
throw StateError(
'Expected parse1 and parse2 to share the identical default list instance, but they did not.',
);
}
// 2. Mutating the list returned by ArgResults mutates the parser's cached default state.
list1.add('injected.dart');
final parse3 = parser.parse([]);
final list3 = parse3.multiOption('include');
if (list3.length != 2 || list3[0] != 'base.dart' || list3[1] != 'injected.dart') {
throw StateError(
'Expected parser default to reflect in-place mutation and contain injected item, got $list3',
);
}
// 3. Multi-options with implicit defaults (empty) also share a mutable list instance.
final implicit1 = parse1.multiOption('flags');
final implicit2 = parse2.multiOption('flags');
if (!identical(implicit1, implicit2)) {
throw StateError(
'Expected implicit default multi-option to share the same mutable instance across parses.',
);
}
implicit1.add('--verbose');
final implicit3 = parse3.multiOption('flags');
if (implicit3.length != 1 || implicit3[0] != '--verbose') {
throw StateError(
'Expected implicit default to retain mutated entry on subsequent parse, got $implicit3',
);
}
// 4. Explicit command line arguments allocate a fresh, unshared list on each parse.
final cliParse = parser.parse(['--include', 'custom.dart']);
final cliList = cliParse.multiOption('include');
if (identical(cliList, list3)) {
throw StateError(
'Expected explicitly parsed multi-option to allocate a fresh list instance, not the default list.',
);
}
if (cliList.length != 1 || cliList[0] != 'custom.dart') {
throw StateError(
'Expected CLI multiOption to contain only parsed arguments, got $cliList',
);
}
// 5. In contrast, ArgResults.arguments and ArgResults.rest are unmodifiable collections.
bool argumentsProtected = false;
try {
cliParse.arguments.add('forbidden');
} on UnsupportedError {
argumentsProtected = true;
}
if (!argumentsProtected) {
throw StateError('Expected ArgResults.arguments to reject mutation with UnsupportedError.');
}
bool restProtected = false;
try {
cliParse.rest.add('forbidden');
} on UnsupportedError {
restProtected = true;
}
if (!restProtected) {
throw StateError('Expected ArgResults.rest to reject mutation with UnsupportedError.');
}
}