# Buggy code
def setup_node_modules(
    production: bool = DEFAULT_PRODUCTION,
    stdout: Optional[IO[Any]] = None,
    stderr: Optional[IO[Any]] = None,
    prefer_offline: bool = False,
) -> None:
    yarn_args = get_yarn_args(production=production)
    if prefer_offline:
        yarn_args.append("--prefer-offline")
    sha1sum = generate_sha1sum_node_modules(production=production)
    target_path = os.path.join(NODE_MODULES_CACHE_PATH, sha1sum)
    cached_node_modules = os.path.join(target_path, "node_modules")
    success_stamp = os.path.join(target_path, ".success-stamp")
    # Check if a cached version already exists
    if not os.path.exists(success_stamp):
        do_yarn_install(
            target_path, yarn_args, success_stamp, stdout=stdout, stderr=stderr
        )

    print("Using cached node modules from %s" % (cached_node_modules,))
    if os.path.islink("node_modules"):
        os.remove("node_modules")
    elif os.path.isdir("node_modules"):
        shutil.rmtree("node_modules")
    os.symlink(cached_node_modules, "node_modules")


# Traceback (subprocess.CalledProcessError)
2020-04-17 21:23:41,590 upgrade-zulip-stage-2: Building static assets...
Cached version not found! Installing node modules.
+ /srv/zulip-yarn/bin/yarn install --non-interactive --frozen-lockfile --prod

Error running a subcommand of ./tools/update-prod-static: /srv/zulip-yarn/bin/yarn install --non-interactive --frozen-lockfile --prod
Actual error output for the subcommand is just above this.

Traceback (most recent call last):
File "./tools/update-prod-static", line 37, in <module>
setup_node_modules(production=True, stdout=fp, stderr=fp)
File "./tools/../scripts/lib/node_cache.py", line 66, in setup_node_modules
stderr=stderr)
File "./tools/../scripts/lib/node_cache.py", line 91, in do_yarn_install
cwd=target_path, stdout=stdout, stderr=stderr)
File "./tools/../scripts/lib/zulip_tools.py", line 200, in run
subprocess.check_call(args, **kwargs)
File "/usr/lib/python3.5/subprocess.py", line 581, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/srv/zulip-yarn/bin/yarn', 'install', '--non-interactive', '--frozen-lockfile', '--prod']' returned non-zero exit status 1
Traceback (most recent call last):
File "/home/zulip/deployments/2020-04-18-01-53-13/scripts/lib/upgrade-zulip-stage-2", line 122, in <module>
preexec_fn=su_to_zulip)
File "/usr/lib/python3.5/subprocess.py", line 581, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['./tools/update-prod-static', '--authors-not-required', '--prev-deploy', '/home/zulip/deployments/current']' returned non-zero exit status 1
Traceback (most recent call last):
File "/home/zulip/deployments/current/scripts/lib/upgrade-zulip-from-git", line 69, in <module>
deploy_path, "--from-git"] + deploy_options)
File "/usr/lib/python3.5/subprocess.py", line 581, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['/home/zulip/deployments/2020-04-18-01-53-13/scripts/lib/upgrade-zulip-stage-2', '/home/zulip/deployments/2020-04-18-01-53-13', '--from-git']' returned non-zero exit status 1

# Fixed code
def setup_node_modules(
    production: bool = DEFAULT_PRODUCTION,
    prefer_offline: bool = False,
) -> None:
    yarn_args = get_yarn_args(production=production)
    if prefer_offline:
        yarn_args.append("--prefer-offline")
    sha1sum = generate_sha1sum_node_modules(production=production)
    target_path = os.path.join(NODE_MODULES_CACHE_PATH, sha1sum)
    cached_node_modules = os.path.join(target_path, "node_modules")
    success_stamp = os.path.join(target_path, ".success-stamp")
    # Check if a cached version already exists
    if not os.path.exists(success_stamp):
        do_yarn_install(target_path, yarn_args, success_stamp)

    print("Using cached node modules from %s" % (cached_node_modules,))
    if os.path.islink("node_modules"):
        os.remove("node_modules")
    elif os.path.isdir("node_modules"):
        shutil.rmtree("node_modules")
    os.symlink(cached_node_modules, "node_modules")