aws-amplify / aws-amplify/amplify-cli

gen2-migration "generate" emits uncompilable data resource — schema.graphql embedded in a TS template literal without escaping (backticks / @ in docstrings)

Open
#14,913 0 comments 0 reactions 0 assignees View on GitHub
pending-triage
Dominant language
TypeScript
Stars
2.9k
Forks
825
Avg merge
11d 23h
Merged PRs (30d)
2

Description

### How did you install the Amplify CLI?

npm

### If applicable, what version of Node.js are you using?

20.19.6

### Amplify CLI Version

@aws-amplify/cli-internal-gen2-migration-experimental-alpha@0.7.0 (host @aws-amplify/cli 14.3.0) — bug is in the experimental gen2-migration alpha, not the core CLI

### What operating system are you using?

macOS

### Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

No manual cloud changes. The docstring containing the backtick / @ is committed in our schema.graphql.

### Describe the bug

When amplify gen2-migration generate (@aws-amplify/cli-internal-gen2-migration-experimental-alpha@0.7.0) renders the Gen 2 data resource, it embeds the contents of schema.graphql verbatim inside a TypeScript template literal (backtick string). Any backtick (`) or @-prefixed identifier inside a GraphQL docstring (""" … """) breaks the generated TypeScript:

- a backtick in a docstring prematurely closes the template literal → cascade of TS syntax errors;
- an @name inside a docstring can be parsed as a misplaced decorator.

The generated data resource then fails to compile. The raw read is in gen1-app.js (file(relativePath)); the unescaped value flows into the data-resource renderer that wraps it in the template literal.

### Expected behavior

The schema string should be escaped for template-literal embedding (at minimum escape ` and ${) before being written into the generated TypeScript, so arbitrary docstring text round-trips and the generated data resource compiles.

### Reproduction steps

1. In a Gen 1 project, add a docstring with a backtick and/or an @-identifier to schema.graphql:
```
"""
Prefer the new field. The `legacy` path is @deprecated.
"""
type Note @model { id: ID! body: String }
```
2. Run npx amplify gen2-migration generate.
3. Open the generated data resource (e.g. amplify/data/resource.ts): the schema template literal is broken (unterminated string from the stray backtick; @deprecated left sitting as a decorator).
4. TypeScript fails to compile the generated file.

### Project Identifier

_No response_

### Log output

```
# Put your logs below this line

```

### Additional information

The correct fix is to escape the schema string before embedding (escape ` and ${). As a non-invasive workaround we sanitize docstrings on read in gen1-app.js — strip leading @ from identifier-like patterns and replace backticks with single quotes inside """ """ blocks (docstrings are descriptions, not directives, so this is cosmetic):

```
file(relativePath) {
- return (0, node_fs_1.readFileSync)(node_path_1.default.join(this.ccbDir, relativePath), 'utf8');
+ let content = (0, node_fs_1.readFileSync)(node_path_1.default.join(this.ccbDir, relativePath), 'utf8');
+ if (relativePath.endsWith('schema.graphql')) {
+ content = content.replace(/"""[\s\S]*?"""/g, (m) =>
+ m.replace(/@(?=[a-zA-Z])/g, '').replace(/`/g, "'")
+ );
+ }
+ return content;
}
```

### Before submitting, please confirm:

- [x] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
- [x] I have removed any sensitive information from my code snippets and submission.

Contributor guide

Open the contributing guide

Research direction

Start in gen1-app.js at file(relativePath), then trace where the schema.graphql value reaches the data-resource renderer and its TypeScript template literal. Reproduce with the provided docstring, ensure backticks and ${ are safe in the generated resource, and verify that amplify gen2-migration generate produces a TypeScript file that compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, node.js, typescript
Domain
backend-api-design, cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
64/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.