Skip to content

Commit

Permalink
perf(js_run_devserver): allow direct file sync
Browse files Browse the repository at this point in the history
  • Loading branch information
ramzesucr committed Nov 13, 2024
1 parent bfddcb3 commit dab5c01
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
3 changes: 3 additions & 0 deletions js/private/js_run_devserver.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ _attrs = dicts.add(js_binary_lib.attrs, {
default = True,
),
"grant_sandbox_write_permissions": attr.bool(),
"direct_sync": attr.bool(),
"allow_execroot_entry_point_with_no_copy_data_to_bin": attr.bool(),
"command": attr.string(),
})
Expand Down Expand Up @@ -91,6 +92,8 @@ def _js_run_devserver_impl(ctx):
config["command"] = ctx.attr.command
if ctx.attr.grant_sandbox_write_permissions:
config["grant_sandbox_write_permissions"] = "1"
if ctx.attr.direct_sync:
config["direct_sync"] = True

ctx.actions.write(config_file, json.encode(config))

Expand Down
28 changes: 16 additions & 12 deletions js/private/js_run_devserver.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ async function deleteFiles(previousFiles, updatedFiles, sandbox) {
}

// Sync list of files to the sandbox
async function syncFiles(files, sandbox, writePerm) {
async function syncFiles(files, sandbox, writePerm, directSync) {
console.error(`+ Syncing ${files.length} files & folders...`)
const startTime = perf_hooks.performance.now()

Expand All @@ -327,10 +327,15 @@ async function syncFiles(files, sandbox, writePerm) {
)
}

let otherFilesSrcDir = RUNFILES_ROOT
if (directSync) {
otherFilesSrcDir = process.env.BUILD_WORKSPACE_DIRECTORY
}

let totalSynced = (
await Promise.all(
otherFiles.map(async (file) => {
const src = path.join(RUNFILES_ROOT, file)
const src = path.join(otherFilesSrcDir, file)
const dst = path.join(sandbox, file)
return await syncRecursive(src, dst, sandbox, writePerm)
})
Expand Down Expand Up @@ -460,11 +465,13 @@ async function main(args, sandbox) {
async function processChunk(chunk) {
try {
const chunkString = chunk.toString()
if (chunkString.includes('IBAZEL_BUILD_COMPLETED SUCCESS')) {
if (process.env.JS_BINARY__LOG_DEBUG) {
console.error('IBAZEL_BUILD_COMPLETED SUCCESS')
}

const triggerSync = chunkString.includes('IBAZEL_BUILD_COMPLETED SUCCESS') || (chunkString.includes('IBAZEL_BUILD_STARTED') && config.direct_sync)

if (process.env.JS_BINARY__LOG_DEBUG) {
console.error('IBAZEL_BUILD_STARTED')
}

if (triggerSync) {
const oldFiles = config.data_files

// Re-parse the config file to get the latest list of data files to copy
Expand All @@ -483,16 +490,13 @@ async function main(args, sandbox) {
syncFiles(
updatedDataFiles,
sandbox,
config.grant_sandbox_write_permissions
config.grant_sandbox_write_permissions,
config.direct_sync,
),
])

// The latest state of copied data files
config.data_files = updatedDataFiles
} else if (chunkString.includes('IBAZEL_BUILD_STARTED')) {
if (process.env.JS_BINARY__LOG_DEBUG) {
console.error('IBAZEL_BUILD_STARTED')
}
}

// Forward stdin to the subprocess. See comment about
Expand Down

0 comments on commit dab5c01

Please sign in to comment.