This repository has been archived by the owner on Jan 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 477
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[doctor] check for sdkVersion in Expo config (#4732)
* [doctor] check for expo.sdkVersion * use @expo/config SDK installed version resolution to compare with Expo config * remove bad mock stuff
- Loading branch information
1 parent
4cd9960
commit b1852a8
Showing
6 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module.exports = require(require.resolve('resolve-from')); | ||
|
||
module.exports.silent = (fromDirectory, request) => { | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
try { | ||
fromDirectory = fs.realpathSync(fromDirectory); | ||
} catch (error) { | ||
if (error.code === 'ENOENT') { | ||
fromDirectory = path.resolve(fromDirectory); | ||
} else { | ||
return; | ||
} | ||
} | ||
|
||
const outputPath = path.join(fromDirectory, 'node_modules', request); | ||
if (fs.existsSync(outputPath)) { | ||
return outputPath; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/expo-doctor/src/checks/ExpoConfigCommonIssueCheck.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import JsonFile from '@expo/json-file'; | ||
import resolveFrom from 'resolve-from'; | ||
|
||
import { DoctorCheck, DoctorCheckParams, DoctorCheckResult } from './checks.types'; | ||
|
||
export class ExpoConfigCommonIssueCheck implements DoctorCheck { | ||
description = 'Check Expo config for common issues'; | ||
|
||
sdkVersionRange = '*'; | ||
|
||
async runAsync({ projectRoot, exp }: DoctorCheckParams): Promise<DoctorCheckResult> { | ||
const issues: string[] = []; | ||
let advice; | ||
|
||
// compare SDK version in package.json with installed expo package version | ||
// If these don't match, it almost certainly means the user specified expo.sdkVersion in their app.json/ app.config.js | ||
const expoSDKVersionFromPackageJson = getExpoSDKVersionFromPackage(projectRoot); | ||
|
||
if (expoSDKVersionFromPackageJson !== exp.sdkVersion) { | ||
issues.push( | ||
"It appears that expo.sdkVersion is defined in your app.json/ app.config.js. This can cause 'expo install' to install dependency versions for the wrong SDK. SDK version is determined by the version of the expo package installed in your project." | ||
); | ||
advice = 'Remove expo.sdkVersion from your app.json/ app.config.js.'; | ||
} | ||
|
||
return { | ||
isSuccessful: issues.length === 0, | ||
issues, | ||
advice, | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* Resolve the Expo SDK Version either from the input Expo config or from the installed | ||
* version of the `expo` package. | ||
* Adapted from https://github.com/expo/expo/blob/main/packages/%40expo/config/src/getExpoSDKVersion.ts | ||
*/ | ||
function getExpoSDKVersionFromPackage(projectRoot: string): string | undefined { | ||
const packageJsonPath = resolveFrom.silent(projectRoot, 'expo/package.json'); | ||
if (!packageJsonPath) { | ||
// (probably) technically impossible - if this happens, `getConfig` throws and Doctor crashes | ||
return undefined; | ||
} | ||
const expoPackageJson = JsonFile.read(packageJsonPath, { json5: true }); | ||
const { version: packageVersion } = expoPackageJson; | ||
|
||
if (!(typeof packageVersion === 'string')) { | ||
// This is technically impossible. | ||
return undefined; | ||
} | ||
|
||
const majorVersion = packageVersion.split('.').shift(); | ||
return `${majorVersion}.0.0`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/expo-doctor/src/checks/__tests__/ExpoConfigCommonIssueCheck.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { vol } from 'memfs'; | ||
|
||
import { ExpoConfigCommonIssueCheck } from '../ExpoConfigCommonIssueCheck'; | ||
|
||
jest.mock('resolve-from'); | ||
jest.mock('fs'); | ||
|
||
const projectRoot = '/tmp/project'; | ||
|
||
// required by runAsync | ||
const additionalProjectProps = { | ||
exp: { | ||
name: 'name', | ||
slug: 'slug', | ||
sdkVersion: '49.0.0', | ||
}, | ||
projectRoot, | ||
}; | ||
|
||
describe('runAsync', () => { | ||
it('returns result with isSuccessful = true if Expo config SDK version matches installed version', async () => { | ||
vol.fromJSON({ | ||
[projectRoot + '/node_modules/expo/package.json']: `{ | ||
"version": "49.0.0" | ||
}`, | ||
}); | ||
|
||
const check = new ExpoConfigCommonIssueCheck(); | ||
const result = await check.runAsync({ | ||
pkg: { name: 'name', version: '1.0.0' }, | ||
...additionalProjectProps, | ||
}); | ||
expect(result.isSuccessful).toBeTruthy(); | ||
}); | ||
|
||
it('returns result with isSuccessful = false if Expo config SDK version does not match installed version', async () => { | ||
vol.fromJSON({ | ||
[projectRoot + '/node_modules/expo/package.json']: `{ | ||
"version": "48.0.0" | ||
}`, | ||
}); | ||
|
||
const check = new ExpoConfigCommonIssueCheck(); | ||
const result = await check.runAsync({ | ||
pkg: { name: 'name', version: '1.0.0' }, | ||
...additionalProjectProps, | ||
exp: { | ||
name: 'name', | ||
sdkVersion: '49.0.0', | ||
slug: 'slug', | ||
}, | ||
}); | ||
expect(result.isSuccessful).toBeFalsy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters