Migrate all TOML I/O callsites to TomlFile#6944
Conversation
Coverage report
Test suite run success3802 tests passing in 1453 suites. Report generated by 🧪jest coverage report action from 80d65bc |
f9b265f to
7e9554f
Compare
31f057f to
80bf13d
Compare
|
We detected some changes at Caution DO NOT create changesets for features which you do not wish to be included in the public changelog of the next CLI release. |
| const regex = /\n?((\s*)type\s*=\s*"\S*")/ | ||
| updatedTomlContents = tomlContents.replace(regex, `$2\nuid = "${extension.uid}"\n$1`) | ||
| // Single extension (or no extensions array): add uid at the top level via WASM patch | ||
| await file.patch({uid: extension.uid}) |
There was a problem hiding this comment.
Single-extension path may write uid at top-level instead of inside [[extensions]]
Previously, the single-extension branch inserted uid before the first type = ... occurrence, which typically lives inside [[extensions]]. Now it does:
await file.patch({uid: extension.uid})This patches uid at the top level rather than inside extensions[0]. Meanwhile, the multi-extension branch inserts uid next to the matching handle inside a specific [[extensions]] table, yielding inconsistent output shape depending on file structure. If downstream expects per-extension [[extensions]].uid, a top-level uid may be ignored or misinterpreted. Additionally, if ('uid' in file.content) return now treats a top-level uid as completed even if per-extension uid is missing.
|
🤖 Code Review · #projects-dev-ai for questions ✅ Complete - 2 findings 📋 History✅ 2 findings |
| } | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| return encodeToml(localExtensionRepresentation as any) | ||
| return localExtensionRepresentation |
There was a problem hiding this comment.
Wait, so then buildTomlObject doesn't build a toml object anymore no? it builds a JSON that might or might not be converted to toml. Should this buildTomlObject return a TomlFile? should we rename it?
There was a problem hiding this comment.
same for all the other buildTomlObject that exist in other files, seems like these are the main changes in this PR (by number of lines), should this function just use TomlFile and in the tests, instead of validating the JSON, validate the TomlFile raw content?
There was a problem hiding this comment.
The nex pr in the stack renames the functions to indicate that it's not specifically toml - I just didn't want to include here as the function reference change would make this pr more indirect. I went with a name change instead of consuming TOML directly but am happy to talk about it. I think it makes sense to pass around object reference and convert to/from toml in narrower pipelines.
7e9554f to
509a528
Compare
82055c8 to
7749fae
Compare
dmerand
left a comment
There was a problem hiding this comment.
This is a great improvement! I left some notes from AI pair review, but they're not blocking.
Review assisted by pair-review
There was a problem hiding this comment.
🧑🍳 💋 to how much simpler this is.
| {keyPath: 'application_url', value: urls.applicationUrl}, | ||
| {keyPath: 'auth.redirect_urls', value: urls.redirectUrlWhitelist}, | ||
| ] | ||
| const configFile = await TomlFile.read(localApp.configuration.path) |
There was a problem hiding this comment.
💡 Improvement: The current implementation builds the patch object with conditional property assignment—creating a mutable patch variable that's modified across different code paths. While clear, this could be more concise using object spread to make the conditional inclusion of app_proxy more explicit and avoid mutating state.
Suggestion: Consider using a single object spread:
const configFile = await TomlFile.read(localApp.configuration.path)
await configFile.patch({
application_url: urls.applicationUrl,
auth: {redirect_urls: urls.redirectUrlWhitelist},
...(urls.appProxy && {
app_proxy: {
url: urls.appProxy.proxyUrl,
subpath: urls.appProxy.proxySubPath,
prefix: urls.appProxy.proxySubPathPrefix,
},
}),
})This makes it clearer that app_proxy is conditionally included and avoids mutating the patch object.
There was a problem hiding this comment.
yeah, there's improvement to be done here. trying to mirror pre-existing behavior 1:1 first and then keep addressing this stuff, rather than mixing concerns.
| await writeFile(path, tomlObject) | ||
| const tomlContent = buildTomlObject(ext, extensions, app.configuration) | ||
| const tomlPath = joinPath(directory, 'shopify.extension.toml') | ||
| const file = new TomlFile(tomlPath, tomlContent as JsonMapType) |
There was a problem hiding this comment.
🐛 Bug: Line 116 creates a TomlFile with tomlContent as the second argument, but line 117 immediately calls replace() with the same content. The TomlFile constructor sets this.content = content, so passing tomlContent to the constructor is redundant since it's overwritten by replace(). This pattern differs from other parts of the codebase where TomlFile is initialized with an empty object before replacement.
Suggestion:
| const file = new TomlFile(tomlPath, tomlContent as JsonMapType) | |
| const file = new TomlFile(tomlPath, {}) |
e13bc32 to
a4d144a
Compare
Replaces scattered setAppConfigValue/setManyAppConfigValues/unsetAppConfigValue with TomlFile.patch/remove. Extension builders return objects instead of TOML strings. writeAppConfigurationFile uses TomlFile.replace + transformRaw for comment injection. breakdown-extensions uses Object.keys() instead of encode-then-regex-parse. Removes decode parameter from loadConfigurationFileContent. encodeToml/decodeToml no longer imported outside cli-kit. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
7749fae to
80d65bc
Compare
Differences in type declarationsWe detected differences in the type declarations generated by Typescript for this branch compared to the baseline ('main' branch). Please, review them to ensure they are backward-compatible. Here are some important things to keep in mind:
New type declarationspackages/cli-kit/dist/public/node/toml/codec.d.tsimport { JsonMap } from '../../../private/common/json.js';
export type JsonMapType = JsonMap;
/**
* Given a TOML string, it returns a JSON object.
*
* @param input - TOML string.
* @returns JSON object.
*/
export declare function decodeToml(input: string): JsonMapType;
/**
* Given a JSON object, it returns a TOML string.
*
* @param content - JSON object.
* @returns TOML string.
*/
export declare function encodeToml(content: JsonMap | object): string;
packages/cli-kit/dist/public/node/toml/index.d.tsexport type { JsonMapType } from './codec.js';
packages/cli-kit/dist/public/node/toml/toml-file.d.tsimport { JsonMapType } from './codec.js';
/**
* Thrown when a TOML file cannot be parsed. Includes the file path for context.
*/
export declare class TomlParseError extends Error {
readonly path: string;
constructor(path: string, cause: Error);
}
/**
* General-purpose TOML file abstraction.
*
* Provides a unified interface for reading, patching, removing keys from, and replacing
* the content of TOML files on disk.
*
* - `read` populates content from disk
* - `patch` does surgical WASM-based edits (preserves comments and formatting)
* - `remove` deletes a key by dotted path (preserves comments and formatting)
* - `replace` does a full re-serialization (comments and formatting are NOT preserved).
* - `transformRaw` applies a function to the raw TOML string on disk.
*/
export declare class TomlFile {
/**
* Read and parse a TOML file from disk. Throws if the file doesn't exist or contains invalid TOML.
* Parse errors are wrapped in {@link TomlParseError} with the file path for context.
*
* @param path - Absolute path to the TOML file.
* @returns A TomlFile instance with parsed content.
*/
static read(path: string): Promise<TomlFile>;
readonly path: string;
content: JsonMapType;
constructor(path: string, content: JsonMapType);
/**
* Surgically patch values in the TOML file, preserving comments and formatting.
*
* Accepts a nested object whose leaf values are set in the TOML. Intermediate tables are
* created automatically. Setting a leaf to `undefined` removes it (use `remove()` for a
* clearer API when deleting keys).
*
* @example
* ```ts
* await file.patch({build: {dev_store_url: 'my-store.myshopify.com'}})
* await file.patch({application_url: 'https://example.com', auth: {redirect_urls: ['...']}})
* ```
*/
patch(changes: {
[key: string]: unknown;
}): Promise<void>;
/**
* Remove a key from the TOML file by dotted path, preserving comments and formatting.
*
* @param keyPath - Dotted key path to remove (e.g. 'build.include_config_on_deploy').
* @example
* ```ts
* await file.remove('build.include_config_on_deploy')
* ```
*/
remove(keyPath: string): Promise<void>;
/**
* Replace the entire file content. The file is fully re-serialized — comments and formatting
* are NOT preserved.
*
* @param content - The new content to write.
* @example
* ```ts
* await file.replace({client_id: 'abc', name: 'My App'})
* ```
*/
replace(content: JsonMapType): Promise<void>;
/**
* Transform the raw TOML string on disk. Reads the file, applies the transform function
* to the raw text, writes back, and re-parses to keep `content` in sync.
*
* Use this for text-level operations that can't be expressed as structured edits —
* e.g. Injecting comments or positional insertion of keys in arrays-of-tables.
* Subsequent `patch()` calls will preserve any comments added this way.
*
* @param transform - A function that receives the raw TOML string and returns the modified string.
* @example
* ```ts
* await file.transformRaw((raw) => `# Header comment\n${raw}`)
* ```
*/
transformRaw(transform: (raw: string) => string): Promise<void>;
private decode;
}
Existing type declarationsWe found no diffs with existing type declarations |

Summary
Migrates every TOML file I/O callsite to use
TomlFile:dev.ts,context.ts,urls.ts:setAppConfigValue/unsetAppConfigValue→TomlFile.patch()/remove()write-app-configuration-file.ts:encodeToml+writeFileSync→TomlFile.replace()+transformRaw()add-uid-to-extension-toml.ts: rawreadFile/writeFile→TomlFile.read()+patch()/transformRaw()breakdown-extensions.ts:encodeToml→ regex → field names replaced withObject.keys()on diff objectsloader.ts: removed unuseddecodeparameter fromloadConfigurationFileContent/parseConfigurationFileenvironments.ts:decodeToml→TomlFile.read()patch-app-configuration-file.ts: removed all TOML functions (onlypatchAppHiddenConfigFileremains)encodeToml/decodeTomlare no longer imported outsidecli-kit.Test plan