Skip to content

#2388 stop empty lines accumulating at the start of ssjs files - #2691

Open
Pushpak731 wants to merge 1 commit into
Accenture:mainfrom
Pushpak731:fix/2388-ssjs-empty-lines
Open

Pushpak731 wants to merge 1 commit into
Accenture:mainfrom
Pushpak731:fix/2388-ssjs-empty-lines

Conversation

@Pushpak731

Copy link
Copy Markdown

Description

Fixes #2388

ssjs files gained one leading empty line per deploy/retrieve roundtrip, so after deploying to multiple BUs each BU's copy showed a different number of empty lines at the top, depending on how many roundtrips it went through.

Root cause (two sides of the same roundtrip):

  • Asset._mergeCode wrapped the deployed .ssjs file content into <script runat="server">…</script> without trimming it, so whatever empty lines the file had were carried into the API payload and stored in SFMC (Script._mergeCode already trims; Asset did not).
  • Util.getSsjs returned the content between the script tags including the newlines the wrapper adds around it, so retrieved/extracted files started with an empty line even for cleanly stored scripts. The Script build path worked around this with code.replace(/^\n/, ''); prettier (when enabled) masked the rest, which is why tests never caught it.

Changes

  • lib/metadataTypes/Asset.js: trim the file content when wrapping it for the API and close the tag on its own line, matching the format Script._mergeCode produces
  • lib/util/util.js: getSsjs strips leading \n/\r\n from the extracted code, so extracted .ssjs files start with the first line of code and accumulated empty lines from previously stored scripts are cleaned up on the next retrieve
  • lib/metadataTypes/Script.js: drop the now-redundant single-leading-newline compensation in _buildForNested
  • @types/lib/util/util.d.ts.map: regenerated via pre-commit hook

How Has This Been Tested?

local tests (new + existing):

  • new test/general.test.js → getSsjs describe: 7 unit tests for the extraction (wrapper newlines, accumulated newlines, CRLF, code directly after the tag, no tags → null, nested script → null, empty input → null) plus a _mergeCode roundtrip test proving the API payload is wrapped exactly once and unwraps back to the original file content (idempotent across further roundtrips)
  • verified the new tests fail on the unfixed code (4 failing before the fix, 8 passing after)
  • full suites still green: general + type.script + type.asset (96 passing) and type.asset.mobile + type.asset.mobile.references + type.asset.references (93 passing)
  • eslint + prettier clean; pre-commit typescript validation passed

Additional context

Roundtrip before the fix (what gets stored in SFMC and grows per deployment):

deploy file:      "\n\nvar a = 1;\n"          <- after two roundtrips on older versions
API payload:      '<script runat="server">\n\n\nvar a = 1;\n</script>'
retrieved file:   "\n\n\nvar a = 1;\n"        <- one more empty line each time

after the fix:

deploy file:      "var a = 1;\n"
API payload:      '<script runat="server">\nvar a = 1;\n</script>'
retrieved file:   "var a = 1;\n"              <- stable from here on

Asset._mergeCode wrapped the deployed ssjs file content into the script tags without trimming it, while Util.getSsjs returned the content between the tags including the newlines the wrapper added. Each deploy/retrieve roundtrip therefore added another empty line to the stored script and to retrieved files, which is why every BU copy ended up with a different number of leading empty lines depending on how often it was deployed.

- trim the file content when wrapping it for the API, matching what Script._mergeCode already does
- strip leading newlines from the extracted code in Util.getSsjs so extracted files start with the first line of code
- drop the now-redundant single-newline compensation in Script._buildForNested
Copilot AI balanced review requested due to automatic review settings October 16, 2026 06:42

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] empty lines added to beginning of ssjs files during build or deploy

2 participants