Skip to content

Commit 7de9983

Browse files
asukaminato0721meta-codesync[bot]
authored andcommitted
fix display the current pyrefly version in the sandbox #3802 (#4435)
Summary: Fixes #3802 export version from cargo env into wasm and insert into tsx Pull Request resolved: #4435 Test Plan: add test Reviewed By: stroxler Differential Revision: D114725315 Pulled By: rchen152 fbshipit-source-id: 17daaaa2718dfabf9f35fe999163168e295f81fe
1 parent 55182c9 commit 7de9983

7 files changed

Lines changed: 94 additions & 0 deletions

File tree

‎pyrefly/lib/lib.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ mod tsp;
5959

6060
pub(crate) use compat::*;
6161

62+
/// The version of this Pyrefly build.
63+
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
64+
6265
/// This interface is NOT stable and should not be relied upon.
6366
/// It will change during minor version increments.
6467
///

‎pyrefly_wasm/lib.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ use wasm_bindgen::prelude::*;
1616

1717
extern crate console_error_panic_hook;
1818

19+
/// Return the version of Pyrefly embedded in this WASM module.
20+
#[wasm_bindgen]
21+
pub fn version() -> String {
22+
pyrefly::VERSION.to_owned()
23+
}
24+
1925
#[wasm_bindgen]
2026
pub struct State(Playground);
2127

‎website/docusaurus.config.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,22 @@ import StylexPlugin from '@stylexjs/webpack-plugin';
1616
import PyodidePlugin from '@pyodide/webpack-plugin';
1717
import path from "path";
1818
import fs from "fs";
19+
import { execSync } from "child_process";
1920
import remarkSandboxPlugin from "./src/sandbox/remarkSandboxPlugin";
2021

2122
const BasePath = 'en/docs';
2223

24+
// The commit is only known in a git checkout of the GitHub repository. Elsewhere, such as in
25+
// fbsource, there is no matching GitHub commit, so it stays empty.
26+
let pyreflyCommit = '';
27+
try {
28+
pyreflyCommit = execSync('git rev-parse HEAD', {
29+
cwd: __dirname,
30+
encoding: 'utf8',
31+
stdio: ['ignore', 'pipe', 'ignore'],
32+
}).trim();
33+
} catch {}
34+
2335
const baseUrl = process.env.DOCUSAURUS_BASE_URL || '/';
2436

2537
const siteUrl = 'https://pyrefly.org';
@@ -290,6 +302,7 @@ const config: Config = {
290302
new webpack.EnvironmentPlugin({
291303
INTERNAL_STATIC_DOCS:
292304
process.env.INTERNAL_STATIC_DOCS === '1' || false,
305+
PYREFLY_COMMIT: pyreflyCommit,
293306
}),
294307
],
295308
};

‎website/src/__tests__/__mocks__/pyreflyWasmMock.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,6 @@ class MockState {
8787
module.exports = {
8888
__esModule: true,
8989
default: (): Promise<void> => Promise.resolve(),
90+
version: (): string => '1.2.3-test',
9091
State: MockState,
9192
};

‎website/src/__tests__/__snapshots__/sandbox.test.tsx.snap‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ exports[`Sandbox Component render sandbox correctly 1`] = `
3636
/>
3737
Parameter hints
3838
</label>
39+
<a
40+
href="https://github.com/facebook/pyrefly/commit/123456789abcdef"
41+
id="pyrefly-build"
42+
rel="noopener noreferrer"
43+
target="_blank"
44+
title="Pyrefly 1.2.3-test, commit 123456789abcdef"
45+
>
46+
Pyrefly 1.2.3-test (commit 123456789)
47+
</a>
3948
</div>
4049
<div>
4150
<div

‎website/src/__tests__/sandbox.test.tsx‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ import { SANDBOX_FILE_NAME } from '../pages/sandbox';
1515
import { DEFAULT_SANDBOX_PROGRAM } from '../sandbox/DefaultSandboxProgram';
1616

1717
describe('Sandbox Component', () => {
18+
const pyreflyCommit = '123456789abcdef';
19+
20+
beforeAll(() => {
21+
process.env.PYREFLY_COMMIT = pyreflyCommit;
22+
});
23+
24+
afterAll(() => {
25+
delete process.env.PYREFLY_COMMIT;
26+
});
27+
1828
test('render sandbox correctly', async () => {
1929
const container = await act(async () => {
2030
const { container } = render(
@@ -31,6 +41,14 @@ describe('Sandbox Component', () => {
3141
DEFAULT_SANDBOX_PROGRAM,
3242
false
3343
);
44+
const buildLink = container.querySelector('#pyrefly-build');
45+
expect(buildLink).toHaveTextContent(
46+
'Pyrefly 1.2.3-test (commit 123456789)'
47+
);
48+
expect(buildLink).toHaveAttribute(
49+
'href',
50+
`https://github.com/facebook/pyrefly/commit/${pyreflyCommit}`
51+
);
3452

3553
// Run test with --update-snapshot to update the snapshot if the test is failing after
3654
// you made a intentional change to the home page
@@ -79,6 +97,25 @@ describe('Sandbox Component', () => {
7997
fireEvent.click(toggle);
8098
expect(toggle).toBeChecked();
8199
});
100+
test('omits the commit for local builds', async () => {
101+
delete process.env.PYREFLY_COMMIT;
102+
try {
103+
const container = await act(async () => {
104+
const { container } = render(
105+
<Sandbox sampleFilename={SANDBOX_FILE_NAME} />
106+
);
107+
108+
await Promise.resolve();
109+
return container;
110+
});
111+
112+
expect(
113+
container.querySelector('#pyrefly-build')
114+
).not.toBeInTheDocument();
115+
} finally {
116+
process.env.PYREFLY_COMMIT = pyreflyCommit;
117+
}
118+
});
82119

83120
function expectMonacoEditorLoadedWithContent(
84121
container: HTMLElement,

‎website/src/sandbox/Sandbox.tsx‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ export default function Sandbox({
128128
const [isHovered, setIsHovered] = useState(false);
129129
const [pythonVersion, setPythonVersion] = useState('3.12');
130130
const [showParameterHints, setShowParameterHints] = useState(false);
131+
const [pyreflyVersion, setPyreflyVersion] = useState<string | null>(null);
132+
const pyreflyCommit = process.env.PYREFLY_COMMIT;
131133
// Absolute pixel height for the editor pane (null = not yet initialized)
132134
const [editorHeight, setEditorHeight] = useState<number | null>(null);
133135
const [isResizing, setIsResizing] = useState(false);
@@ -564,6 +566,18 @@ export default function Sandbox({
564566
/>
565567
Parameter hints
566568
</label>
569+
{pyreflyVersion && pyreflyCommit && (
570+
<a
571+
id="pyrefly-build"
572+
href={`https://github.com/facebook/pyrefly/commit/${pyreflyCommit}`}
573+
title={`Pyrefly ${pyreflyVersion}, commit ${pyreflyCommit}`}
574+
target="_blank"
575+
rel="noopener noreferrer"
576+
{...stylex.props(styles.commit)}
577+
>
578+
{`Pyrefly ${pyreflyVersion} (commit ${pyreflyCommit.slice(0, 9)})`}
579+
</a>
580+
)}
567581
</div>
568582
);
569583

@@ -670,6 +684,7 @@ export default function Sandbox({
670684
pyreflyWasmInitializedPromise
671685
.then((pyrefly) => {
672686
try {
687+
setPyreflyVersion(pyrefly.version());
673688
setPyreService(new pyrefly.State(pythonVersion));
674689
setLoading(false);
675690
setInternalError('');
@@ -1677,6 +1692,16 @@ const styles = stylex.create({
16771692
whiteSpace: 'nowrap',
16781693
userSelect: 'none',
16791694
},
1695+
commit: {
1696+
color: 'var(--color-text-secondary)',
1697+
fontSize: '12px',
1698+
padding: '7px 15px',
1699+
textDecoration: 'none',
1700+
whiteSpace: 'nowrap',
1701+
':hover': {
1702+
textDecoration: 'underline',
1703+
},
1704+
},
16801705
actionButton: {
16811706
border: 'none',
16821707
background: 'transparent',

0 commit comments

Comments
 (0)