aws-amplify / aws-amplify/amplify-hosting
Inconsistent `$PATH` when using `revalidate = 0` on App router handler
- Dominant language
- Dockerfile
- Stars
- 481
- Forks
- 123
- PR merge metrics
- No merged PRs in 30d
Description
### Before opening, please confirm:
- [X] I have checked to see if my question is addressed in the [FAQ](https://github.com/aws-amplify/amplify-hosting/blob/master/FAQ.md).
- [X] I have [searched for duplicate or closed issues](https://github.com/aws-amplify/amplify-hosting/issues?q=is%3Aissue+).
- [X] I have read the guide for [submitting bug reports](https://github.com/aws-amplify/amplify-hosting/blob/master/CONTRIBUTING.md).
- [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.
### App Id
d179m7yexeqiqu
### AWS Region
us-west-1
### Amplify Hosting feature
Environment variables
### Frontend framework
Next.js
### Next.js version
13.5.4
### Next.js router
App Router
### Describe the bug
When creating API routes using the new route handler in the App router, it seems that adding `export const revalidate = 0` causes some strange effects on the environment. I ran into this issue because I needed to run `execSync('aws ...')` and I run into this error:
```
2023-10-24T19:47:16.901+02:00 | ⨯ Error: Command failed: aws amplify list-jobs --region eu-west-1 --app-id --branch-name
2023-10-24T19:47:16.901+02:00 | /bin/sh: aws: command not found
```
So I tried to check `export $PATH` and see what it returns. On the file with `export const revalidate = 0`, the value may differ from one refresh to another, but it seems this inconsistency in `$PATH` is causing the issue.
```
# Problematic path that does not have `aws` binary from `echo $PATH`
/var/lang/bin:/usr/local/bin:/usr/bin/:/bin:/opt/bin
```
Here is a recording from an API endpoint with `revalidation = 0` set, notice that the `$PATH` output varies from one refresh to another:
https://github.com/aws-amplify/amplify-hosting/assets/5593067/774a3d3d-0013-45bc-ba28-8ef3d013716c
While when no revalidation segment is added, then the `$PATH` appears normal (and that is also where `aws` binary is accessible):
```
# This is the correct path with aws binary
/tmp/xfs-c139e862:/usr/local/rvm/gems/ruby-2.7.6/bin:/usr/local/rvm/gems/ruby-2.7.6@global/bin:/usr/local/rvm/rubies/ruby-2.7.6/bin:/usr/local/rvm/bin:/root/.nvm/versions/node/v18.13.0/bin:/root/.local/bin:/root/.local/bin:/root/.local/bin:/usr/local/rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/.dotnet/tools
```
### Expected behavior
I expect `echo $PATH` to be consistent even when `revalidation: 0` is set on a route segment.
### Reproduction steps
Create a new route in the Next.js app, e.g. `/app/api/test-revalidate/route.ts`:
```ts
// test-revalidate/route.ts
import { execSync } from "child_process";
export const GET = async (req: Request) => {
try {
const path = execSync('echo $PATH');
return Response.json({ path });
} catch (error) {
return Response.json({ error: 'Error encountered' }, { status: 500 });
}
};
```
After an Amplify Preview has been deployed, check the route at `/api/test-revalidate` and notice that the JSON output varies from one load to another.
You can compare this to an identical route that has no revalidation defined:
```ts
// test-no-revalidate/route.ts
import { execSync } from "child_process";
// This is the line that causes inconsistent `$PATH` value
export const revalidate = 0;
export const GET = async (req: Request) => {
try {
const path = execSync('echo $PATH');
return Response.json({ path });
} catch (error) {
return Response.json({ error: 'Error encountered' }, { status: 500 });
}
};
```
This API route `/api/test-no-revalidate` will always return a consistent `$PATH` value.
### Build Settings
_No response_
### Log output
```
# Put your logs below this line
```
### Additional information
_No response_
Contributor guide
Research direction
Start with the reproduced App Router handlers at /app/api/test-revalidate/route.ts and the identical route without revalidation. Deploy an Amplify Preview, request both endpoints repeatedly, and compare the PATH returned by execSync('echo $PATH'). Done means the route with revalidate = 0 has the same consistent PATH behavior as the control route and does not lose access to the aws binary.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, next.js, typescript
- Domain
- backend, cloud, devops
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100